473,804 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NULL==0?

Hi,

Are these equivalent:

char text[];

if(text==NULL){ }
if(text==0){}

Thank you,
Q
Oct 10 '06 #1
20 3804

"Quantum" <no*****@addres s.comwrote in message
news:6N******** **********@news fe1-gui.ntli.net...
Hi,

Are these equivalent:

char text[];

if(text==NULL){ }
if(text==0){}

Thank you,
Q
Although it's from the C faq it addresses your question
http://c-faq.com/null/index.html

Serafeim
Oct 10 '06 #2
Papastefanos Serafeim wrote:
"Quantum" <no*****@addres s.comwrote in message
news:6N******** **********@news fe1-gui.ntli.net...
>Hi,

Are these equivalent:

char text[];

if(text==NULL) {}
if(text==0){ }

Thank you,
Q

Although it's from the C faq it addresses your question
http://c-faq.com/null/index.html

Serafeim

Ok, thanks. :)
Q
Oct 10 '06 #3
Quantum wrote:
Are these equivalent:

char text[];

if(text==NULL){ }
if(text==0){}
yes, but, since text is an array it can never be NULL...
--
Nick Keighley

Oct 10 '06 #4
Nick Keighley wrote:
Quantum wrote:
>Are these equivalent:

char text[];

if(text==NULL) {}
if(text==0){ }

yes, but, since text is an array it can never be NULL...

Quantum wrote:
>Are these equivalent:

char text[];

if(text==NULL) {}
if(text==0){ }

yes, but, since text is an array it can never be NULL...

Good point! I'm passing text[] into a function, however it is still
declared as a pointer. :)
Oct 10 '06 #5
Quantum wrote:
Hi,

Are these equivalent:

char text[];

if(text==NULL){ }
if(text==0){}
Strictly speaking they are equivalent, but idiomatically, the use of the
NULL macro indicates that you are evaluating a pointer, and not just
evaluating some integer.

Comparing to NULL and comparing to an integer value of zero are
equivalent in you example, but they are not equivalent everywhere NULL
may be used. This is because NULL does not require a typecast in order
to be passed as a parameter to a function that expects a pointer, but a
plain int would not necessarily satisfy the type requirements of the
function prototype. You might still get away with this, because all
your target platforms probably really do have a NULL pointer that is an
integer value with "all bits off", and all your target platforms
probably really do represent the integer value of zero as a two's
complement number with "all bits off." But, this is really just a happy
coincidence. What happens on a platform where "zero" is something else?
How about on a one's complement system where there are TWO zeros? And
what happens on a platform where the NULL pointer, while represented in
the high level language as "zero", really is some other value, like the
address of some hardware trap with a segment and offset value, different
for each program?

So, this doesn't happen on x86, Sparc, ARM, MIPS, PPC, 68K, or any other
machine you are likely to be using. And maybe it's irrelevant to ask if
you'd bet your life on it remaining so. But it is nonetheless
appropriate to practice the good habit of using the NULL macro for the
null pointer.

Oct 11 '06 #6
jmcgill wrote:
Quantum wrote:
>Hi,

Are these equivalent:

char text[];

if(text==NULL) {}
if(text==0){ }

Strictly speaking they are equivalent, but idiomatically, the use of the
NULL macro indicates that you are evaluating a pointer, and not just
evaluating some integer.

Comparing to NULL and comparing to an integer value of zero are
equivalent in you example, but they are not equivalent everywhere NULL
may be used. This is because NULL does not require a typecast in order
to be passed as a parameter to a function that expects a pointer, but a
plain int would not necessarily satisfy the type requirements of the
function prototype. You might still get away with this, because all
your target platforms probably really do have a NULL pointer that is an
integer value with "all bits off", and all your target platforms
probably really do represent the integer value of zero as a two's
complement number with "all bits off." But, this is really just a happy
coincidence. What happens on a platform where "zero" is something else?
How about on a one's complement system where there are TWO zeros? And
what happens on a platform where the NULL pointer, while represented in
the high level language as "zero", really is some other value, like the
address of some hardware trap with a segment and offset value, different
for each program?

So, this doesn't happen on x86, Sparc, ARM, MIPS, PPC, 68K, or any other
machine you are likely to be using. And maybe it's irrelevant to ask if
you'd bet your life on it remaining so. But it is nonetheless
appropriate to practice the good habit of using the NULL macro for the
null pointer.
Actually, IIRC, assigning or initializing a pointer type to zero is
defined to produce a null pointer. So, in the case above, 0 would be
promoted to a char* and become a null char*, regardless of the
implementation' s actual representation of null pointers. I could be
wrong, but I don't think I am.

Nate
Oct 12 '06 #7
jmcgill wrote:
Strictly speaking they are equivalent, but idiomatically, the use of the
NULL macro indicates that you are evaluating a pointer, and not just
evaluating some integer.
Well many people use the idiomatic zero constant expression for the
null pointer, so you've got no guarantee.
This is because NULL does not require a typecast in order
to be passed as a parameter to a function that expects a pointer, but a
plain int would not necessarily satisfy the type requirements of the
function prototype.
A plain int must be cast, but a NULL POINTER CONSTANT, such as the naked
zero there behaves exactly the same as NULL.
You might still get away with this, because all
your target platforms probably really do have a NULL pointer that is an
integer value with "all bits off",
It's got absolutely nothing to do with the representation of the
pointer. The null pointer constant, defined to be an integral constant
expression evaluating to zero, converts to a pointer type. Any other
integer value, regardless of the format of pointers on your machine,
will NOT convert implicitly. The program is ill-formed IN ALL CASES
if you pass a non-null pointer constant integer value.

The only way you even begin to having to think about what the null
pointer representation is, is when you use a reinterpret cast or
some other non-type safe bashing of the zero value into a pointer.
So, this doesn't happen on x86, Sparc, ARM, MIPS, PPC, 68K, or any other
machine you are likely to be using.
If you're going to bet your life on anything, you might try learning
some of the fundamental concepts of the language.
Oct 12 '06 #8
Quantum wrote:
Nick Keighley wrote:
Quantum wrote:
Are these equivalent:

char text[];
This is a syntax error
Good point! I'm passing text[] into a function, however it is still
declared as a pointer. :)
It isn't declared as anything because your attempted declaration
is a syntax error. A pointer declaration would be:

char *text;

or an array declaration would be:

char text[1];

or some other number instead of 1.

Oct 12 '06 #9
jmcgill wrote:
Strictly speaking they are equivalent, but idiomatically, the use of the
NULL macro indicates that you are evaluating a pointer, and not just
evaluating some integer.
Idiomatically, I like to use if(!ptr) because it looks the same as if
it was a boolean, and it makes sense to me: "if not ptr" is like "if
there's nothing in ptr" or "if ptr is not pointing to anything".
Comparing to NULL and comparing to an integer value of zero are
equivalent in you example, but they are not equivalent everywhere NULL
may be used.
They are always equivalent. It's guaranteed by the standard.
This is because NULL does not require a typecast in order
to be passed as a parameter to a function that expects a pointer, but a
plain int would not necessarily satisfy the type requirements of the
function prototype.
NULL cannot be defined as anything else than zero in C++ so that is
incorrect. NULL is the same as plain 0 as far as overloading is
concerned.
You might still get away with this, because all
your target platforms probably really do have a NULL pointer that is an
integer value with "all bits off", and all your target platforms
probably really do represent the integer value of zero as a two's
complement number with "all bits off." But, this is really just a happy
coincidence. What happens on a platform where "zero" is something else?
How about on a one's complement system where there are TWO zeros? And
what happens on a platform where the NULL pointer, while represented in
the high level language as "zero", really is some other value, like the
address of some hardware trap with a segment and offset value, different
for each program?
You're confusing stuff. In a C++ program the constant 0 IS the null
pointer. It doesn't matter what the representation of it is in the
hardware. If you use 0 in a pointer expression it will have the correct
representation. This is NOT the same as memcmp-ing with zero or
memset-ing a pointer to zero.
So, this doesn't happen on x86, Sparc, ARM, MIPS, PPC, 68K, or any other
machine you are likely to be using. And maybe it's irrelevant to ask if
you'd bet your life on it remaining so. But it is nonetheless
appropriate to practice the good habit of using the NULL macro for the
null pointer.
Talk for yourself. IMO, it is not a good habit to use any unnecessary
macro, which NULL is. NULL is a historical accident like several other
things in the language.

Regards,
Bart.

Oct 13 '06 #10

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

Similar topics

26
45459
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
2133
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
4258
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
7663
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
6078
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
3772
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
24733
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
3962
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
5409
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
3699
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
10603
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...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
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
4314
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
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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.