this is just a small question on NULL. In the snippet below, why doesn't the do-while loop repeat when I don't enter anything for the string?

#include <stdio.h>
#include <string.h>
#include <cs50.h>

int main(int argc, char* argv[])
    string name;

        printf("Enter your name: ");
        name = GetString();
    }while(name == NULL);

    printf("%s\n", name);

EDIT: I checked if GetString() returns null by inserting the following into the do-while loop:

if (name == NULL) printf("Returned NULL");

The workaround was

{same content)while(*name == '\0')

Any now I can't even remember how the workaround worked, do strings automatically get a newline inserted at the beginning when they're initialized?


  • because in this case GetString actually returns NULL.
    – kzidane
    Oct 24, 2015 at 18:45
  • I feel like I'm missing something extremely obvious. I've edited the loop so it'd give me an indication if name == NULL, but apparently GetString isn't returning NULL. I found a workaround and made the rest of initials.c, but I'm still curious as to why this didn't work.
    – user10001
    Oct 24, 2015 at 19:35
  • may I know what the workaround is and why do you think it doesn't return NULL? you may edit your original post!
    – kzidane
    Oct 24, 2015 at 20:30


You must log in to answer this question.

Browse other questions tagged .