Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > NULL in C

Reply
Thread Tools

NULL in C

 
 
Serve La
Guest
Posts: n/a
 
      10-18-2003
C programmers get accused of writing platform dependant code when they write
int *x = 0;
They have to write
int *x = NULL;
because some platforms don't represent NULL as all bits zero.

C++ programmers don't seem to have this problem, they can write 0 whenever
they want. Why is that?


 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      10-18-2003
Serve La wrote:
> C programmers get accused of writing platform dependant code when they write
> int *x = 0;
> They have to write
> int *x = NULL;
> because some platforms don't represent NULL as all bits zero.


0 is a null pointer constant and is guaranteed to yield a null pointer
when used in a pointer context, regardless of the actual bit pattern.

Jirka

 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      10-18-2003
"Serve La" <(E-Mail Removed)> wrote in message
news:bmrjb2$nfv$(E-Mail Removed)1.nb.home.nl...
> C programmers get accused of writing platform dependant code when they write
> int *x = 0;
> They have to write
> int *x = NULL;
> because some platforms don't represent NULL as all bits zero.
>
> C++ programmers don't seem to have this problem, they can write 0 whenever
> they want. Why is that?


Actually, NULL is supposed to be 0. Conforming C compilers
recognize the value 0 (when used as a pointer) to be a
special value that means NULL. Even when the implementation
internally uses some other binary value to mean a NULL
pointer, the value 0 is what is used the source code. This
includes assignment, comparison, dereferencing, and casting
between integer and pointer types. The compiler translates
from 0 in the source code into its internal representation
of a NULL pointer.

This is why you can write:

if(x)
{
// x is non-NULL
}

which is exactly the same as:

if(NULL != x)
{
// x is non-NULL
}

which is also exactly the same as:

if(0 != x)
{
// x is non-NULL
}

The C compiler knows that 0 is being used in a pointer-type
expression and does the right thing.

--
----------------------------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
303-774-9381
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!


 
Reply With Quote
 
Mark Gordon
Guest
Posts: n/a
 
      10-18-2003
On Sat, 18 Oct 2003 14:53:03 GMT
"xarax" <(E-Mail Removed)> wrote:

> "Serve La" <(E-Mail Removed)> wrote in message
> news:bmrjb2$nfv$(E-Mail Removed)1.nb.home.nl...
> > C programmers get accused of writing platform dependant code when
> > they write int *x = 0;
> > They have to write
> > int *x = NULL;
> > because some platforms don't represent NULL as all bits zero.
> >
> > C++ programmers don't seem to have this problem, they can write 0
> > whenever they want. Why is that?

>
> Actually, NULL is supposed to be 0.


Actually it is not required to be 0. (void*)0 is also a valid definition
of NULL.

> Conforming C compilers
> recognize the value 0 (when used as a pointer) to be a
> special value that means NULL.


Only a constant expression evaluation to 0. I don't think an integer
containing a value 0 is guaranteed by the standard because it would be a
pain to implement if the null pointer was not all bits zero.

> Even when the implementation
> internally uses some other binary value to mean a NULL
> pointer, the value 0 is what is used the source code. This
> includes assignment, comparison, dereferencing, and casting
> between integer and pointer types. The compiler translates
> from 0 in the source code into its internal representation
> of a NULL pointer.


<snip>

the rest looked about right.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
 
Reply With Quote
 
Mathew Hendry
Guest
Posts: n/a
 
      10-18-2003
On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <(E-Mail Removed)> wrote:

>C programmers get accused of writing platform dependant code when they write
>int *x = 0;
>They have to write
>int *x = NULL;
>because some platforms don't represent NULL as all bits zero.


The accusers are misinformed. This is a common source of confusion. NULL and
0 are interchangable with very few exceptions.

>C++ programmers don't seem to have this problem, they can write 0 whenever
>they want. Why is that?


The main difference between C and C++ in this area is that in C++, NULL must
be defined as

#define NULL 0

or equivalent. The alternative permitted in C

#define NULL (void *)0

is not acceptable in C++ because C++ does not allow implicit conversions
from void * to other pointer types. The C++ people viewed these implicit
conversions as a hole in the type system, and closed it.

-- Mat.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-18-2003
"Serve La" <(E-Mail Removed)> writes:
> C programmers get accused of writing platform dependant code when they write
> int *x = 0;
> They have to write
> int *x = NULL;
> because some platforms don't represent NULL as all bits zero.
>
> C++ programmers don't seem to have this problem, they can write 0 whenever
> they want. Why is that?


The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.
Section 5 is about null pointers.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Scott Fluhrer
Guest
Posts: n/a
 
      10-18-2003

"Mathew Hendry" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <(E-Mail Removed)> wrote:
>
> >C programmers get accused of writing platform dependant code when they

write
> >int *x = 0;
> >They have to write
> >int *x = NULL;
> >because some platforms don't represent NULL as all bits zero.

>
> The accusers are misinformed. This is a common source of confusion. NULL

and
> 0 are interchangable with very few exceptions.

int i = 0; /* Groovy */
int j = NULL; /* An error on some compilers */



--
poncho


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-18-2003
Serve La wrote:
> C programmers get accused of writing platform dependant code when they write
> int *x = 0;


If they are so accused, it is by idiots.

> They have to write
> int *x = NULL;


If they are so required, it is by idiots.

> because some platforms don't represent NULL as all bits zero.


This is not a reason and is completely irrelevant in pointer contexts.

> C++ programmers don't seem to have this problem,


Neither do C programmers.

> they can write 0 whenever they want.


As can C programmers.

> Why is that?


I don't know why your collection of at best half-truths exists. Your post
is one of the worst attempts at a troll I've ever seen.



--
Martin Ambuhl

 
Reply With Quote
 
keanu
Guest
Posts: n/a
 
      10-18-2003
> I don't know why your collection of at best half-truths exists. Your post
> is one of the worst attempts at a troll I've ever seen.


Yours on the other hand is pritty good.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-18-2003
"Scott Fluhrer" <(E-Mail Removed)> wrote:

>
>"Mathew Hendry" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <(E-Mail Removed)> wrote:
>>
>> >C programmers get accused of writing platform dependant code when they

>write
>> >int *x = 0;
>> >They have to write
>> >int *x = NULL;
>> >because some platforms don't represent NULL as all bits zero.

>>
>> The accusers are misinformed. This is a common source of confusion. NULL

>and
>> 0 are interchangable with very few exceptions.

>int i = 0; /* Groovy */
>int j = NULL; /* An error on some compilers */
>
>


Er, seems you're just joking, but anyway: the discussion was about
0 vs. NULL in pointer contexts.

int *i = 0; /* valid */
int *j = NULL; /* more idiomatic variant */
printf("%p",0); /* undefined behaviour */

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
Is there a null ostream (like /dev/null) in cpp? Bo Peng C++ 13 07-18-2004 07:17 PM



Advertisments