Thread: explain me about NULL

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Question explain me about NULL

    someone pliz help me to explain what is NULL
    i'm so confused about ut
    thx b4

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    NULL is a macro defined in <stddef.h> to be a null pointer constant. This means that it is typically defined as 0 or ((void*)0).
    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

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    It's also defined in <stdio.h>

    EDIT: FAQ
    Last edited by BEN10; 11-26-2009 at 08:31 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by laserlight View Post
    NULL is a macro defined in <stddef.h> to be a null pointer constant. This means that it is typically defined as 0 or ((void*)0).
    Extention of this
    C
    #define NULL 0

    C++

    #define NULL ((void*)0)

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    hmm
    thx u all
    but I still don't understand what is utility of NULL
    sry
    but I'm still beginner in programming ^^

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RockyMarrone
    Extention of this
    C
    #define NULL 0

    C++

    #define NULL ((void*)0)
    No, NULL is implementation defined, and a standard conforming C++ implementation will not define NULL as ((void*)0) in <cstddef>.

    Quote Originally Posted by firedream
    but I still don't understand what is utility of NULL
    What do you understand of pointers and 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

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by firedream View Post
    hmm
    thx u all
    but I still don't understand what is utility of NULL
    sry
    but I'm still beginner in programming ^^
    NULL simply means that no memory is allocated to the pointer. For eg.
    Code:
    int *p; //uninitialized pointer which is dangerous
    int *p=NULL; // no memory is allocated better than the previous one
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BEN10 View Post
    NULL simply means that no memory is allocated to the pointer.
    Right -- because the value of a pointer is a memory address. Guess where 0 is?

    You could also assign "2" to a pointer and it will respond like it is NULL, because 2 is not a valid address either.

    One useful thing about NULL is that it is 0. 0 is the only value in C that will fail a truth test:
    Code:
    char *ptr = NULL;
    if (ptr) {
        blah blah
    "blah blah" will not happen if ptr == 0 (NULL). Notice that many C functions will return a NULL pointer to indicate an error or some kind of failure. Eg, if you use strstr(), which searches for a substring (a pattern, such as "wo") in a longer string (such as "hello world") you get a pointer to the beginning of "wo" if it is found, and a NULL pointer if it is not. If it is found you probably want to do something that you will not want to do otherwise, so the truth test is very useful -- really, it is the basic and fundamental concept in programming.

    Nb., a pointer assigned and invalid address (like 2) is effectively NULL and will behave that way but it will pass a truth test. So will an uninitialized pointer. Which is why we use NULL
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @MK27
    Code:
    int *ptr;
    ptr=10; //or any number
    Will ptr be allocated no memory in this case too? It's surprising!!
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Right -- because the value of a pointer is a memory address. Guess where 0 is?
    No, semantically a null pointer does not point to any memory location, not even address 0.

    Quote Originally Posted by MK27
    You could also assign "2" to a pointer and it will respond like it is NULL, because 2 is not a valid address either.
    (...)
    Nb., a pointer assigned and invalid address (like 2) is effectively NULL and will behave that way but it will pass a truth test. So will an uninitialized pointer. Which is why we use NULL
    Not quite:
    Quote Originally Posted by C99 Section 6.3.2.3 Paragraphs 3 to 5
    An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

    Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.

    An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.
    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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BEN10 View Post
    Will ptr be allocated no memory in this case too? It's surprising!!
    Technically, looking at the quote from laserlight (the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation) I suppose it is more or less "undefined" what will happen, but realistically 2 is not a valid address so there can be no memory there.

    Another interesting point about that correction is that two NULL pointers will not == one another, which I had not noticed this, so there is bit more to NULL than it just being

    #define NULL 0

    which I thought it was exactly that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Another interesting point about that correction is that two NULL pointers will not == one another
    Eh, I think you misread. The standard states that "any two null pointers shall compare equal", not that they shall not compare equal.
    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

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Eh, I think you misread. The standard states that "any two null pointers shall compare equal", not that they shall not compare equal.
    I guess that's why I hadn't noticed. I was thinking I had done that, too (compared NULL pointers). It seems to me that is more useful, so good!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM