473,883 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
18 154380
"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_ELEM S * 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*****@earthl ink.net> wrote in message
news:_J******** *********@newsr ead2.news.atl.e arthlink.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.ne tcom.com> wrote:

"Mathew Hendry" <md****@blueyon der.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*****@earthl ink.net> wrote in message
news:_J******* **********@news read2.news.atl. earthlink.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*******@free net.de)
Nov 13 '05 #15
Joe Wright <jo********@ear thlink.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
implementati on has defined 'NULL 0' then (void*)NULL will be required.


That's the point.
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #16
In article <bm**********@n ews1.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.na me> 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_Keit h) 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.loca ldomain> Micah Cowan <mi***@cowan.na me> 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
45465
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.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
3
2139
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 Perl, and Nulls have started to bother me greatly. The first issue is, as far as I understand it, a column should be NOT NULL if it is necessary (required) data. Now, if a column doesn't have to be NOT NULL; that is, it's not _required_ data,...
5
4263
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 = qryEvent.EventID WHERE ProdID = Forms!frmProductions!ProdID))) ORDER BY qryRole.Role; If there is just one RoleID with a null value in the subquery then the main
3
7667
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
6130
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" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
29
3784
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 compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to allocate a block of memory, preinitialising it to zero, is this equivalent (and portable C) to...
5
24748
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
64
3967
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")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work just like `void* p=NULL'. Is this correct?
0
5417
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 EndRequest (by turning response buffering off) then HttpContext.Current is set. To repo add the following to Global.asax protected void Application_PreSendRequestHeaders(object sender, EventArgs e) {...
46
3705
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
9934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9789
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10840
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7969
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5795
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4608
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.