Thread: 0 vs Null

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    0 vs Null

    I am a little confused about the difference between zeros and nulls in C. My programming friend (who uses C++ if that matters) says that in C they are the same thing. But if i was reading values out of an array or reading an array wouldnt the array stop being read at a null? If they were the same my array would stop being read at a zero.

    A problem arose when I was passing an array from another language into a C library. If I passed in zeros the C function would not write to the array but if i passed in ones for instance everything was fine.

    Does this make any sense?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by chico1st View Post
    I am a little confused about the difference between zeros and nulls in C. My programming friend (who uses C++ if that matters) says that in C they are the same thing. But if i was reading values out of an array or reading an array wouldnt the array stop being read at a null? If they were the same my array would stop being read at a zero.

    A problem arose when I was passing an array from another language into a C library. If I passed in zeros the C function would not write to the array but if i passed in ones for instance everything was fine.

    Does this make any sense?
    the NULL has ASCII value of 0.on the other hand 0 is just an integer and array indexing starts with 0 in C.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by chico1st View Post
    I am a little confused about the difference between zeros and nulls in C. My programming friend (who uses C++ if that matters) says that in C they are the same thing.
    They are the same thing in this sense: if you test the "truth" of a variable, it is considered false if the value is 0 or NULL, eg:
    Code:
    int X=0;
    char *ptr=NULL;
    if (X) {won't happen, because X is false};
    if (!(ptr)) {will happen, because "ptr" is not(!) true}
    But if i was reading values out of an array or reading an array wouldnt the array stop being read at a null? If they were the same my array would stop being read at a zero.
    If you are talking about an array of ints, no, 0 is not null or ignored. It is the integer 0. If you are talking about a character string, '\0' (the null terminator) has a value of 0, and that is where the string is considered to end. So string functions will stop reading the array there. But the character 0 ('0', not '\0') is not the same thing (it actually has a ASCII value of 48).
    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

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Quote Originally Posted by chico1st View Post
    I am a little confused about the difference between zeros and nulls in C. My programming friend (who uses C++ if that matters) says that in C they are the same thing. But if i was reading values out of an array or reading an array wouldnt the array stop being read at a null? If they were the same my array would stop being read at a zero.

    A problem arose when I was passing an array from another language into a C library. If I passed in zeros the C function would not write to the array but if i passed in ones for instance everything was fine.

    Does this make any sense?
    You may want to take a look at this:

    Cprogramming.com FAQ > NULL, 0, \0 and nul?

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I believe NULL used to be defined as:
    Code:
    #define NULL  (void*)0
    I'm not sure if that's still the case though.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by cpjust View Post
    I believe NULL used to be defined as:
    Code:
    #define NULL  (void*)0
    I'm not sure if that's still the case though.
    NULL can always be replaced with 0, as C guarantees to implicitly convert 0 to a void pointer to 0. Hence it's perfectly valid to define NULL this way:

    Code:
    #define NULL 0
    The standard talks explicitly about that:

    An integer constant expression with the value 0, or such an expression cast to type
    void *, is called a null pointer constant.
    When defining 0 as (void*)0, I'd suggest putting parentheses around the expression, i.e. ((void*)0).

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

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, 03:25 AM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03: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-14-2004, 11:06 PM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM