I know in C you can declare a string and the number of characters like below,
char mystring[50];
with ’50’ being the number of characters.
However, what is proper procedure if the user is going to be inputting the contents of the string (via scanf(“%s”, mystring);)? Do I leave it as,
char mystring[0];
leaving it as ‘0’ since I have no clue how many characters the user will input?
Or do I do,
char mystring[400];
giving up to 400 characters for the user to input?
You’ve hit upon the exact problem with scanf() and %s – what happens when you don’t know how much input there is?
If you try running
char mystring[0];, your program will compile just fine. But you will always segfault. You’re creating an array of size 0, so when you try to place something into that array, you will immediately go out of bounds for your string (since no memory will have been allocated) – which is a segfault.So, point 1: you should always allocate a size for your string. I can think of very few circumstances (okay, none) where you would want to say
char mystring[0]rather thanchar *mystring.Next, when you use scanf, you never want to use the “%s” specifier – because this will not do any bounds-checking on the size of the string. so even if you have:
if the user enters more than 511 characters (since the 512th is \0), you will go out of the bounds of your array. The way to remedy this is:
This is all to say that C doesn’t have a facility to automatically resize a string if there is more input than you’re expecting. This is the kind of thing you have to do manually.
One way to deal with this is by using fgets().
You could say:
You may then use sscanf() to parse
mystringTry the above code, with a string of length 5. After 4 characters have been read, that code loops again to retrieve the rest of the input. “Processing” could include code to re-allocate a string to be a bigger size and then append the newest input from fgets().
The above code isn’t perfect – it would make your program loop and process any infinite string length, so you might want to have some internal hard limit on that (eg, loop a maximum of 10 times).