In C, I want to take an input from the user but I don't know how long is it. I think I should use malloc() to set the length of the text array:
char *text = NULL;
text = malloc(sizeof(input))
But, how can I do it at the same time that I'm storing the input to the array with fgets()?
fgets(text, sizeof text, stdin)
fgets, limiting the length to the size of the buffer. If it reads less, then you can callreallocto reduce the size and you're done. But if there might be more, then callreallocto double the size of the buffer, continue reading, and repeat as needed.char* text = malloc(sizeof(input)). That is, unlessinputhappens to be a statically allocatedchararray (in which case, since you know the size of the static array, you would most likely not need to allocate another one dynamically).sizeof textis the size of the pointer - irrelevant to reading user input.