0

I'm trying to create a program that displays the string length of an entered string with a pointer to aforementioned string. When I input a string without spaces, the length is correct. When I enter a string with spaces, the length is incorrect. Please describe my mistake and recommend ways to fix it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int l,n;
    char s[n], *p=s;
    *p = malloc(sizeof(char)*5);
    n = sizeof(*p);
    if (p==NULL)
    printf("Unable to allocate memory");

    printf("Input a string: ");
    scanf("%s", p);
    l = strlen(p);
    printf("\nLength of given string: %d\n", l);
}

Input:

Input a string: June 27

Output:

Length of given string: 4

4
  • 1
    You allocated p with a length of 5. You're overflowing your buffer. Commented Jun 28, 2020 at 1:53
  • You allocated enough space for four characters and a null byte. You entered seven characters. Things are not going to work well. Commented Jun 28, 2020 at 1:54
  • 1
    char s[n];, *p = malloc(sizeof(char)*5); Each of those should be generating at least a warning, which you shouldn't ignore. Either that, or what you posted is not the real code. Commented Jun 28, 2020 at 1:55
  • scanf("%s"... reads to the first whitespace, and you are responsible for providing the memory, so it's a good thing you put a space after "June". Commented Jun 28, 2020 at 1:56

2 Answers 2

1

Aside from the obvious memory allocation shortcomings mentioned in the comments, "%s" used with scanf will read up to the first whitespace. If you want to read complete lines (up to newline), consider using fgets instead.

Sign up to request clarification or add additional context in comments.

Comments

0

The program has the following defects:

  1. The variable n is uninitialized before its usage in char s[n].

  2. You're trying to read the whole line by scanf() which only reads to the next whitespace.

  3. Allocated memory of 5 length, which is an overflow and tends to Undefined Behavior.

  4. The (char *) is not explicitly defined during memory allocation.


Code redefined:

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

int main(void) {
    char *s;

    s = (char *)malloc(sizeof(char) * MAX); // allocating memory correctly

    if (s == NULL) {
        printf("Memory allocation failed.\n");
        return -1;
    }

    printf("Enter a string: ");
    fgets(s, MAX, stdin); // reading the entire line

    size_t len = 0;

    while (s[len++]); // alternative of strlen()

    printf("String length is: %d\n", len);

    return 0;
}

A sample output:

$ gcc -o prog prog.c; ./prog
Enter a string: Hello world, how are you today?
String length is: 33

4 Comments

In C it's frowned upon to cast the void* result of malloc(). Just let the compiler do the right thing.
Why did you invent your own strlen()?
@thebusybee I invented own strlen() to reduce the program size by not adding the entire header file just for a simple function. If the thing could be done in simple, then let it be done.
The size of a header file says nothing about the growth of the resulting binary. I can add zillions of bytes of header files without adding any byte to the executable. Did you compare your DIY solution with the provided strlen() to back up your claim?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.