So im learning about pointers and dynamic memory and how am experimenting with fgets.
So i want to take in a string input into a pointer using fgets, but i want fgets size to change dynamically with malloc function that i used for the pointer is there a way to do it ? e.g.
int main(){
char *text;
text =(char *)malloc(200 * sizeof(char));
fgets(text, n, stdin);
return 0;
}
Explanation
i have tried fgets(text, sizeof(text), stdin);
but it does not work ?
This will not work since sizeof(ptr)
where ptr is a dynamic pointer to a char array will always be size of pointer (8 for 64bits machines), not size of array. You would have to:
char text[200];