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 than char *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:
char mystring[512];
scanf("%s", mystring);
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:
scanf("%511s", mystring);
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:
while (fgets(mystring, 512, stdin))
{
/* process input */
}
You may then use sscanf() to parse mystring
Try 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).