Thread: Null

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Null

    Hi, I'm wondering about NULL pointer how its exactly work in C!!!!!!

    In brief, NULL pointer means that there's no deferencing to any other byte's addresses, so what I can conclude is this following syntax writing as:
    Code:
    char *str="CBOARD"
    while (*str!=NULL) 
    {/*..do something..*/
    str++
    }
    is valid in C, but I can't write and change the loop's while condition with
    Code:
     while (str==NULL)
    Am I totally write or just missed up the concept of NULL pointer??!!!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Read this: Question 5.1
    More here: Null Pointers

    Why not make a complete example?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *str = "CBOARD";
    
        while (*str != NULL)
        {
            /*..do something..*/
            str++;
        }
    
        return 0;
    }
    
    /*
        main.c|7|warning: comparison between pointer and integer
    */
    As you can see, your first example generates a warning because you're dereferencing the pointer for the comparison.

    Your second example should actually work:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *str = "CBOARD";
    
        while (str == NULL)
        {
            printf("In the loop\n");
            str++;
        }
    
        return 0;
    }
    There is no output because "str" is pointing somewhere - it is not NULL.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think that this is the umpteenth time that you have confused a null character with a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    28
    *"abc" == 'a'
    *"bc" == 'b'
    *"c" == 'c'
    *"" == '\0'

    Now if you understand that, understand that you're guaranteed "" != NULL and that any address of any object is guaranteed to compare unequal to NULL. So there are no null pointers here, not even in ""+1.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    For what it's worth, I prefer to call a null pointer NULL, and the C end of string mask (or null character) NUL, with just one L. This matches the C usage for the pointer, and the ASCII (and Unicode) tables for the character.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nominal Animal
    For what it's worth, I prefer to call a null pointer NULL (...) This matches the C usage for the pointer
    But in an ironic twist, NULL isn't actually a null pointer, and '\0' is a null pointer constant!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by laserlight View Post
    NULL isn't actually a null pointer
    True, it just expands to one. (I hope that was your point, because this is C and not C++ we're talking about. )

    Quote Originally Posted by laserlight View Post
    '\0' is a null pointer constant
    Not really; it's an integer (and thus not a pointer) constant, which you can use as a pointer constant.

    The difference is subtle, like saying that a cream pie is a comic device. To me, it's not, it's just a delicious dessert. (I don't find food fights or pieing or similar antics funny at all, just a waste of sweet edibles to me.)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nominal Animal
    True, it just expands to one. (I hope that was your point, because this is C and not C++ we're talking about.)
    Haha, referring to macro replacement would be an additional layer to the joke, but I was aiming for something simpler, i.e., NULL as a null pointer constant, but not a null pointer. It doesn't always hold true in C because NULL could be of type void* and hence actually a null pointer, but the ironic twist remains since it could hold true.

    Quote Originally Posted by Nominal Animal
    Not really; it's an integer (and thus not a pointer) constant, which you can use as a pointer constant.
    No, '\0' really is a null pointer constant: "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant." It is true that '\0' is a character constant, but as an expression it is an integer constant expression with the value 0, hence it is a null pointer constant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by laserlight View Post
    It doesn't always hold true in C because NULL could be of type void* and hence actually a null pointer
    Yeah; I've only seen it defined as ((void *)0) (even on old Unix boxes), so I didn't catch that.

    Quote Originally Posted by laserlight View Post
    No, '\0' really is a null pointer constant
    Is it? Because "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant" obviously requires the cast?

    (At this point, we're so deep in the semantics and language interpretation that my English skills are definitely not up to this. I think I might just be having fun teasing you. If so, apologies. )

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nominal Animal
    Is it? Because "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant" obviously requires the cast?

    (At this point, we're so deep in the semantics and language interpretation that my English skills are definitely not up to this. I think I might just be having fun teasing you. If so, apologies. )
    Nah, it is all in good fun. In the first place, the only people who would be using '\0' when they want a null pointer constant would be those writing for an obfuscated C contest.

    Anyway, no: because of the "or", the statement says that there are two kinds of expressions that are called null pointer constants:
    • An integer constant expression with the value 0
    • An integer constant expression with the value 0 that is cast to type void *

    But clearly, an integer constant expression with the value 0 that is not cast to a pointer type is just an integer constant expression, so it would be strange to call it a pointer. Yet, despite this strangeness, the standard calls it a "null pointer constant". Hence, there are null pointer constants that are not null pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by laserlight View Post
    Anyway, no: because of the "or", the statement says that there are two kinds of expressions that are called null pointer constants:
    Okay, that makes sense. (That is, you're right. The standard does not make much sense, but that's nothing new.)

    To RyanC, your initial post indicates you've probably confused the two (NULL and '\0').

    Consider this "safe" variant of strlen():
    Code:
    size_t length(const char *const s)
    {
        size_t n = 0;
    
        if (s != NULL) {
            while (s[n] != '\0')
                n++;
        }
    
        return n;
    }
    It differs from the strlen() function in that this one checks for the NULL pointer (and returns 0 in that case).

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I guess the reason NULL is defined, in C99 at least, as the less type strict version of the null pointer constant because implicit casting makes it okay. How unfortunate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign null value to a struct and check if it is null
    By ymc1g11 in forum C Programming
    Replies: 10
    Last Post: 11-01-2012, 03:58 PM
  2. Replies: 9
    Last Post: 10-20-2007, 01:05 AM
  3. null an int[]
    By Tommo in forum C Programming
    Replies: 4
    Last Post: 07-09-2007, 07:14 AM
  4. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM
  5. What does NULL mean?
    By Sentral in forum C++ Programming
    Replies: 19
    Last Post: 10-29-2005, 11:22 PM