4

According to the lecture in Week 2 and the walkthrough video "String", to make sure that the user gives us a valid string we use this condition:

    if (s != NULL)
    {  
         //code
    }

David said, "it turns out per GetString's own documentation, GetString could on occasion return NULL, a special sentinel value that essentially indicates that the user did not cooperate and somehow did not provide a string."

But when I tried using this condition and didn't put any string as input my code still continued as if I had entered a valid string. I tried to input numbers and special characters instead of string but it also continued as if I had entered a valid string.

Here is a screenshot of my code and the outputs:

So what does this condition actually do? How the user can input a NULL string? Or is there something I am doing wrong?

How can I make sure that the user did input a valid string, not numbers, not special characters and not an empty string?

3

So what actually this condition does?

this condition basically tests whether the char pointer (aka a string) s points to nothing (i.e., address 0x0 in the computer's memory) which is the address of a special location in memory that we can't read from/write to it.

You'll know more about NULL, strings and pointers as you reach week 4. For now, take for granted that when s is equal to NULL, this means that no string exists at all.

how the user can input a NULL string?

well, the user doesn't actually inputs NULL. Rather, as the professor said, she doesn't co-operate somehow. For example, since we have a limited amount of memory, if the user inputs a string that's too long to fit in the available memory we have, GetString() will return NULL and the string is never stored.

Another example would be if the user inputted a special value known as the end-of-file (aka EOF). The end-of-file is a special value that indicates that there's no more data to read and I guess this is what the professor was referring to by "did not provide a string". In most Linux systems, this value is entered by pressing Ctrl + D from your keyboard.

When you don't input anything and press enter, the string that's stored is actually an empty string (i.e., "").

I tried to input numbers and special characters instead of string but it also continued as if I had entered a valid string.

The numbers and special characters you inputted are interpreted as a whole string. Recall that a string is an array of chars where a char could be an uppercase letter, a lowercase letter, a digit or a special character. Recall also that the number 123 is NOT the same as the string "123" because the string "123" is actually an array of the digit chars '1', '2' and '3' where the number 123 is the integral value one hundred and twenty three.

How can I make sure that the user did input a valid string, not numbers, not special characters and not an empty string?

If you wanna ensure that a string contains only alphabetical characters, you'll probably need to iterate over the characters of this string checking whether any of them is a non-alphabetical char. If so, you may take an appropriate action.

Functions declared in ctype.h (e.g., isalpha()) may prove useful for this process.

Hope that helps!

2
  • Thanks a lot! that was very helpful – Michael Nageh Sep 13 '14 at 1:17
  • Thanks for the explanation @Kareem. Is it also possible to wrap s = GetString(); in a do { } while (s == NULL); instead of using if (s != NULL) later? – Cathryne Mar 28 '16 at 9:08
1

Here's the GetString documentation that David is referencing in that lecture, from the CS50 library 3.0:

/*
 * Reads a line of text from standard input and returns it as a
 * string (char *), sans trailing newline character.  (Ergo, if
 * user inputs only "\n", returns "" not NULL.)  Returns NULL
 * upon error or no input whatsoever (i.e., just EOF).  Leading
 * and trailing whitespace is not ignored.  Stores string on heap
 * (via malloc); memory must be freed by caller to avoid leak.
 */

If you enter nothing and only hit the Enter key, you're sending "\n" to the standard input stream, not NULL. The easiest way to generate a NULL value from this function is to send the EOF (or "end of file") character by pressing Ctrl + D in the appliance when prompted for input. If you do that, you should trigger the else block of your code.

1
  • Thanks a lot Thomas, and thanks for the library – Michael Nageh Sep 13 '14 at 1:19
0

Well, looking at this page, it has this to say on GetString():

What’s the deal with the s != NULL? It turns out that the GetString will not always succeed in getting a string from the user. If it fails, perhaps because the user typed a string that was too long to hold in memory, GetString will return a special sentinel value named NULL. Without this check, other things we try to do with s might cause the program to crash.

So the program still works correctly in your first case, it's just that it's returning "", an empty string. You could change the if statement to something like

if(s != NULL && s != "")

if you're looking for a non-blank value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .