0

I'm trying to write a function that can be called for user input, therefore I can just call it whenever and it can be assigned to a variable. However, for some reason it seems to be capping the input at 8 characters, regardless of if it is above or below 8 characters.

char * userInput(){
    char * user_input;
    user_input = malloc(sizeof(char)*100);
    printf("Type > ");
    scanf("%s",user_input);
    printf("%ld",sizeof(user_input));
    return user_input;
}

output

Type > TheIsTheSentence

8Hello TheIsThe

Sam Hall
  • 315
  • 2
  • 8
  • 4
    `sizeof(user_input)` returns the size of the _pointer_ `user_input`, which on a 64 bit architecture is 8 bytes. You probably want `strlen(user_input)` instead to get the length of the string _contained_ in `user_input` (i.e. what `user_input` points to) – Craig Estey Aug 17 '19 at 16:30
  • 1
    you would need to share your output code as well. The input looks ok besides the already mentioned sizeof(user_input) – Bernhard Jaeger Aug 17 '19 at 16:34
  • @Ber Isn't the output code included there? (printf) – user202729 Aug 17 '19 at 16:35
  • was talking about the part "Hello TheIsThe". Also you might want to make it explicit how many characters you want to read as you allocated memory for 100. So scanf("%99s",user_input); – Bernhard Jaeger Aug 17 '19 at 16:42
  • Notice that there is absolutely no benefit in writing `char * user_input; user_input = malloc(sizeof(char)*100);` instead of `char * user_input = malloc(sizeof(char)*100);` – Antti Haapala -- Слава Україні Aug 17 '19 at 17:13

2 Answers2

2

The problematic statement is this

printf("%ld",sizeof(user_input)); /* It desn't return the number of char stored in user_input  */

as user_input is a character pointer and sizeof pointer is always 4 byte on 32 bit machine and 8 bytes on 64 bit machine.

My suggestion is to use fgets() instead of scanf(). For e.g

Declare a character pointer and allocate memory.

char * user_input = NULL;
user_input = malloc(sizeof(*user_input) * MAX_BUF_SIZE); /* Instead of magic number use macro */ 

Do proper error handling of malloc(). For e.g

if(user_input == NULL) {
    fprintf(stderr, "memory allocation failed\n");
    exit(-1);
}

Scan the input using fgets() instead of scanf(). for e.g

size_t retStrCspn = 0;
if(fgets(user_input, MAX_BUF_SIZE, stdin) != NULL) {
   /* fgets success. next task is remove the trailing \n char read by fgets() */
   user_input[retStrCspn = strcspn(user_input, "\n")] = 0; /* remove the trailing & use the return value */
}

Now print the length, don't use sizeof, use the return value of strcspn() for example

printf("%zu", retStrCspn);
Achal
  • 11,821
  • 2
  • 15
  • 37
1

Instead of:

printf("%ld",sizeof(user_input));

Write:

printf("%ld",strlen(user_input));

Note that your program will behave badly if you write more than 99 characters in the input.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • Even better (if your compiler supports it) is `%zu`. `sizeof` and `strlen` return type `size_t`, which is compatible with type `unsigned` on some machines, `unsigned long` on others. But `%zu` takes care of this. – Steve Summit Aug 17 '19 at 17:01