I want to take a string as user input in my program….
...
char* name;
...
printf("\n\tEnter a string : ");
fflush(stdin);
//gets(name);
//gets_s(name,100);
//fgets(name,100,stdin);
...
All the three ways of getting a string input are giving errors.Yes I can take a char array but my requirement is that input string could be of any length. How can I get my requirement fulfilled.
The 100 in gets_s/fgets is just to see whether these functions also complain or not.
I am using VS2010.
EDIT: I added visual c++ tag to show that I am using VC++ but my program is in C.
Thanks
In C, there is no way if you want to handle “input string that could be of any length”. You have to allocate enough memory to store the input; that is you have to specify maximum input characters your program wants to handle.If you want to handle “input string that could be of any length”, you can use
C++std::string, for example:And your example program is wrong, you should allocate enough buffer by a call to
mallocfirst before you usenameto get user input: