Thread: How to check if char* is empty or null??

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    38

    How to check if char* is empty or null??

    Ok - bit new to C - all I want to do is check if a char* is empty or null, in this case I'm checking *roadNumber. It keeps crashing when I run the code saying - "The instruction at blah de blah...The memory could not be read" - so I take it there is some conflict with two variables or something...
    If anyone can help that would be great.
    (BTW - the code is a conditional statement to transfer the contents from a char* to a char array, if roadNumber is empty do this bit of code, else do the other).

    Code:
    if (roadNumber = NULL)
    {
       for(i=0;i<length;i++)
       {
       oneRoadName[i] = propRoadName[i];
       }
    }
    else
    {
      for(i=0;i<length;i++)
      {
      oneRoadName[i] = roadNumber[i];
      }
    }
    Last edited by earnshaw; 12-14-2004 at 01:48 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    if (roadNumber = NULL)
    this is assigning NULL to roadNumber and then evaulating it (which will be false) and in the else block its trying to dereference a NULL pointer. You want:
    Code:
    if (roadNumber == NULL)

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    WOW - thanks that was quick...and oh my god, how thick am I...my excuse, its been a long day.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Hang on - Im debugging the else..if loop and the roadNumber value is showing "" - so it should go into the first part of the loop and do its stuff, but it doesnt- do you know what Im doing wrong??

    PS - I have changed the code to reflect:

    Code:
    if (roadNumber == NULL)
    and even


    Code:
    if (roadNumber == "")
    but it still doesnt work.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This will never, ever, be true:
    Code:
    if (roadNumber == "")
    Use if(roadNumber == NULL) to test if roadNumber is NULL and use if(*roadNumber = '\0') to test if roadNumber is empty.

    EDIT: P.S. Try changing your code to start with:
    Code:
    if(roadNumber == NULL)
      roadNumber = propRoadName;
    ...and see if you can figure out how that might be able to change the way the code you pasted.
    Last edited by itsme86; 12-15-2004 at 03:57 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Hmmmm, thanks for that, I was sure that would work, however it still skipped past, so just to elaborate the actual value inside roadNumber is:

    Code:
    0x00951d80 " "
    32 '  '
    ...bearing in mind the Visual Studio C++ IDE watch dialog.

    I would have thought these values indicate emptiness, is that not so?
    Last edited by earnshaw; 12-15-2004 at 04:16 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It would help if we could see a bit more of the code. Perhaps you're confused on terminology.
    Code:
    char *s = NULL; /* create a pointer, and make it point to NULL (ie: nothing) */
    If you do not initialize a pointer, it does not by default point to null, it points some place randomly, which will more often than not cause you problems. As such, it's always best to make sure your pointers are pointing some place when you create them. The "some place" in this instance happens to be NULL. (Which techincly means "no place", but that's better than "random place".

    Code:
    char *s = ""; /* Create a pointer and have it point to an empty string. */
    This is not the same as NULL. NULL means it points specificly to no place at all. This however is different. This is in fact pointing to an empty string literal. That's not null.

    Code:
    char *s = "Hello!"; /* Create a pointer pointing at a string literal. */
    char *end = s + strlen( s ); /* Jump to the null character in the string */
    Here, the pointer end is pointing to the end of the string. A string by definition contains a number of characters, terminated by a null character. NULL and a null character are not the same thing. A string contains a null character (or null terminator) which denotes the end of the string.

    That is not the same thing as a pointer pointing to NULL.

    A null character is the character '\0'. This is not the same thing as NULL. One you're seeing if the character itself is "nothing", and the other you're testing a pointer to see if the pointer points to "nowhere".

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Thanks for all your help, I totally understand what you are saying. I initialize the pointer to "", but during the code the pointer changes value, it is filled I suspect with a single white-space - so I think this is totally different again from a null or empty string. I think if it was empty it was "" , it would show
    Code:
    0 ' '
    but it shows
    Code:
    32 ' '
    the code is actually querying an SQL stream and I think it is returning a value from an empty row in a column, and this has been misleading me to think its empty and my test to qualify this is slightly wrong.
    Am I making sense?, do I need to test for a single whitespace, and how can I be sure that this test will work everytime?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, how are you reading the input? Something like fgets? Well I suppose it doesn't matter. Just loop through the string (until you hit a null character), comparing the current character with whitespace. (Use something like isspace which is included in ctype.h to do the check.) If you reach any character that isn't white space, you know you have something valid and useable.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    </life>
    Join Date
    Oct 2004
    Posts
    83
    It is only empty if it doesnt contain a character (except \0), if the value is " " then it contains a space, which is a character.

    Check if its NULL, Empty (only '\0'), if its not either see if it contains other characters besides a white space.
    Last edited by dagdarian; 12-15-2004 at 05:27 AM.
    Microsoft is merely an illusion, albeit a very persistant one.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Code:
    if (roadNumber = NULL)
    {
       for(i=0;i<length;i++)
       {
       oneRoadName[i] = propRoadName[i];
       }
    }
    else
    {
      for(i=0;i<length;i++)
      {
      oneRoadName[i] = roadNumber[i];
      }
    }
    try playing with this code to reduce redundancy..this assumes you
    are using null terminated strings and that you have allocated the proper amount of memory for oneRoadName.

    Code:
    char *ptr, *target = oneRoadName;
    if(roadNumber)
       ptr = propRoadName;
    else
       ptr = roadNumber;
    
    while(*ptr)
    {
        *target = *ptr;
        target++;
        ptr++;
    }
    (if anyone thinks this is bad please let me know, i'm still learning myself)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    This seems to work:

    Code:
    if (*roadNumber == ' ')
    {
       for(i=0;i<length;i++)
       {
       oneRoadName[i] = propRoadName[i];
       }
    }
    else
    {
      for(i=0;i<length;i++)
      {
      oneRoadName[i] = roadNumber[i];
      }
    }
    ....is this acceptable code??

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    if it works it works...but would you want to type that for loop twice if it had, oh say, 100 lines in it?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  4. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  5. Replies: 7
    Last Post: 06-16-2006, 09:23 PM