What does NULL mean?

This is a discussion on What does NULL mean? within the C++ Programming forums, part of the General Programming Boards category; What exactly does NULL mean? An example and explanation of it in code, would be cool. But a simple overview ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    What does NULL mean?

    What exactly does NULL mean? An example and explanation of it in code, would be cool. But a simple overview is cool too.

    Thanks...

  2. #2

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well here's a definition: http://en.wikipedia.org/wiki/Null
    Say there is a function that has an extra parameter that you can use to set additional options. If you don't want those additional options you can pass NULL for that parameter. NULL is kind of like a spaceholder for nothing.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't think that's quite right. You wouldn't use NULL where a function expects an int, but if the function expects a pointer (and can handle a NULL value) then you could pass NULL.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    null = 0. "Real" C++ programmers use 0 not NULL.
    Last edited by 7stud; 10-25-2005 at 09:07 PM.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by 7stud
    null = 0. "Real" C++ programmers use 0 not NULL.
    Correction NULL = 0 not null

  7. #7

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    NULL means absolutely nothing
    Check out my blog!
    http://www.om3ga.co.uk

  9. #9
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Sentral,
    NULL = 0, or hex 0x00000000, or binary 000000000000. Its stated in the compiler, #define NULL 0, try hovering over NULL in Visual C++, u'll see how it defines it. When you set a pointer to NULL you are actually setting it to address 0x00000000 which is a placeholder address that points to nada or shorthand - 0.

    7stud,
    I use NULL for an int sometimes when I'm lazy. You sayin I'm not a "real" programmer? Them's fightin words!
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  10. #10
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Not quite right that NULL is kind of like a place holder for nothing? How so? I said kind of because NULL=0 and 0 is important so I wouldn't consider it nothing, although it represents nothing. I usually use 0 if the function is asking for an integer value, but sometimes I use NULL, it all depends.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    From the faq:
    NULL is to be used for pointers only since it may be defined as ((void *)0), this would cause problems with anything but pointers.
    Although, if you can show me that the standard says NULL must be defined as 0 then you would be right
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Bjarne Stroustrup , the father of c++ language, says
    In C++, the definition of NULL is 0
    and says that c++ programs should not use the NULL macro.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Although, if you can show me that the standard says NULL must be defined as 0 then you would be right
    Quote Originally Posted by C++ Standard, 18.1.4
    The macro NULL is an implementation-defined C + + null pointer constant in this International Standard (4.10).180)
    180) Possible definitions include 0 and 0L, but not (void*)0.
    Quote Originally Posted by C++ Standard, 4.10.1
    A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to
    zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that
    type and is distinguishable from every other value of pointer to object or pointer to function type. Two null
    pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer
    to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification
    conversion (4.4).
    Basically NULL doesn't point to "nothing" because 0 is "something". What it does is points to a location that you know you can't use.

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I stand corrected
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Ancient Dragon
    Bjarne Stroustrup , the father of c++ language, says


    and says that c++ programs should not use the NULL macro.
    BS says:
    I prefer to avoid macros, so I use 0.
    So it's a personal preference.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 04:43 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-15-2004, 12:06 AM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 04:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21