>> SPEAKER 1: Let's now fix that last program.
And this time, let's explicitly allocate some memory in which the
user's input will be stored.
To do so, let's hone in on that first line of code where we declared s
previously to be a char star.
This time, let's re-declare it as follows--
char s bracket 16, for instance, close bracket.
>> In other words, let's declare s to no longer be an address of a character,
but rather an array of 16 characters.
This way, the user can type in up to 15 characters and still leave room for
a null terminator.
Let's proceed to Save, Compile, and Run this program.
Make scanf2 dot slash scanf2.
Let's now type a string like hello, and we're thanked for the hello.
>> Now, there's still a problem.
I only typed in hello, which is only five characters, plus 1 for the null
terminator.
It leaves us with only a need for six bytes.
>> But unfortunately, we only allocated 16 in total.
So if the user actually types in 16 characters, or 17, or several hundred
characters, we're still not going to have enough room in memory for the
user's input.
And in fact, this is what makes getting user input so
difficult in general.
And indeed, this is why we implemented the get string function in the first
place in the CS50 library--
to figure out how to handle those situations where the user types in
more characters than we initially anticipated.
>> Frankly, without completely rewriting this program,
there's no clean solution.
Rather, what we would have to do is get a character from the user one at a
time, again and again.
And at each point if we realize we're out of memory, we would have to at
that point go back in and reallocate some more memory, copy the user's
previous input from our first chunk of memory into the new,
larger chunk of memory.
And then repeat that process again and again until the user is done providing
his or her input.