entering string variable size
I'm making program that asks user to enter some string. I use char *string;
gets(string);
I' m wondering if this is a good way to get string from user and if there is possibility that user enter string so big to overwrite some other later data in my program and cause run-time error, because string is pointer to first byte of data.
one of the possible solution is
char string[80];
this will force compiler to set aside memory but then I'll have fixed size of string.
Thank you
Re: entering string variable size
Quote:
Originally posted by Micko
I'm making program that asks user to enter some string. I use char *string;
gets(string);
I' m wondering if this is a good way to get string from user and if there is possibility that user enter string so big to overwrite some other later data in my program and cause run-time error, because string is pointer to first byte of data.
one of the possible solution is
char string[80];
this will force compiler to set aside memory but then I'll have fixed size of string.
Thank you
Forget the malloc
Forget the linked list
The only way to read in a string at your level of knowledge is to use fgets() as suggested and specify a char string[MX];
where MX is the maximum size of your potential input. If you're worried about someone typing too much, make MX = 2048 -- there ain't nobody gonna type that many characters from the keyboard.
All normal string input must go into a pre-defined buffer unless you are at a level of skill to deal with dynamic buffers, direct character input, etc. Learn the basics first.