473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

NULL in C

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?
Nov 13 '05 #1
18 154286
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

Nov 13 '05 #2
"Serve La" <i@bleat.nospam.com> wrote in message
news:bm**********@news1.tilbu1.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!
Nov 13 '05 #3
On Sat, 18 Oct 2003 14:53:03 GMT
"xarax" <xa***@email.com> wrote:
"Serve La" <i@bleat.nospam.com> wrote in message
news:bm**********@news1.tilbu1.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.
Nov 13 '05 #4
On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <i@bleat.nospam.com> 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.

Nov 13 '05 #5
"Serve La" <i@bleat.nospam.com> 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) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #6

"Mathew Hendry" <md****@blueyonder.co.uk> wrote in message
news:6l********************************@4ax.com...
On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <i@bleat.nospam.com> wrote:
C programmers get accused of writing platform dependant code when they writeint *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
Nov 13 '05 #7
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

Nov 13 '05 #8
> 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.
Nov 13 '05 #9
"Scott Fluhrer" <sf******@ix.netcom.com> wrote:

"Mathew Hendry" <md****@blueyonder.co.uk> wrote in message
news:6l********************************@4ax.com.. .
On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <i@bleat.nospam.com> wrote:
>C programmers get accused of writing platform dependant code when theywrite >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
(ir*******@freenet.de)
Nov 13 '05 #10
"Serve La" <i@bleat.nospam.com> 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?


Neither do C programmers. The fact that NULL is not represented
as all bits zero is:

1) Not limited to C. This is also true of C++.
2) Not relevant to the initializations you have above.

Where it becomes a problem is more in code such as:

void **foo = calloc(NUM_ELEMS * sizeof *foo);
/* ... Code that assumes that foo[0] through foo[NUM_ELEMS-1]
are NULL pointers due to the all-bits-zero initialization from
calloc()... */

HTH,
Micah
Nov 13 '05 #11
Serve La wrote:
C programmers get accused of writing
platform dependant code when they write
int *x = 0;
The accusers are ignorant.
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?


Your question depends on an invalid hypothesis.

--
pete
Nov 13 '05 #12
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:_J*****************@newsread2.news.atl.earthl ink.net...
> 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.


Troll!?!? More misinformed than trolling and it was somebody here who said
it BTW.

Here's the worst troll. "Pascal is better than C"
Nov 13 '05 #13
Irrwahn Grausewitz wrote:

"Scott Fluhrer" <sf******@ix.netcom.com> wrote:

"Mathew Hendry" <md****@blueyonder.co.uk> wrote in message
news:6l********************************@4ax.com.. .
On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <i@bleat.nospam.com> 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 */

The variadic printf() requires (void*)0 because the compiler doesn't
parse the format string and doesn't know the context of 0. If the
implementation has defined 'NULL 0' then (void*)NULL will be required.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #14
"Serve La" <i@bleat.nospam.com> wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:_J*****************@newsread2.news.atl.earth link.net...
> 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.


Troll!?!? More misinformed than trolling and it was somebody here who said
it BTW.


If somebody here said so, he has most certainly been corrected.
Or maybe you just misunderstood something.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #15
Joe Wright <jo********@earthlink.net> wrote:
Irrwahn Grausewitz wrote:

<snip>
0 vs. NULL in pointer contexts.

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

The variadic printf() requires (void*)0 because the compiler doesn't
parse the format string and doesn't know the context of 0. If the
implementation has defined 'NULL 0' then (void*)NULL will be required.


That's the point.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #16
In article <bm**********@news1.tilbu1.nb.home.nl>,
"Serve La" <i@bleat.nospam.com> 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.

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


That's not a problem of the C language, that is a problem with people
who don't understand it.

Come back when you understand the difference between an integer zero, a
value with a representation of all bits zero, a null pointer, a null
pointer constant and the NULL macro.
Nov 13 '05 #17
Micah Cowan <mi***@cowan.name> writes:
[...]
Neither do C programmers. The fact that NULL is not represented
as all bits zero is:

1) Not limited to C. This is also true of C++.
2) Not relevant to the initializations you have above.


To be precise, NULL is not *necessarily* represented as all bits zero.
On many implementations, NULL is represented as all bits zero (which
can make it more difficult to detect bugs caused by code that makes
this assumption).

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #18
In <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
Neither do C programmers. The fact that NULL is not represented
as all bits zero is:


NULL is a macro. Its usual representations are 0 or ((void *)0).
If you mean null pointer, NULL is *not* a valid abbreviation.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
3
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in...
5
by: Mike MacSween | last post by:
This as the row source for a combo: SELECT qryRole.RoleID, qryRole.Role FROM qryRole WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN qryEvent ON qryRoleEvent.EventID...
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
29
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
0
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before...
46
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.