Q
Quantum
Hi,
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
Thank you,
Q
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
Thank you,
Q
Quantum said:Hi,
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
Thank you,
Q
Papastefanos said:Quantum said: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
Quantum said:Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
Nick said:Quantum said:Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
yes, but, since text is an array it can never be NULL...
Quantum said:Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
yes, but, since text is an array it can never be NULL...
Quantum said:Hi,
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
jmcgill said:Quantum said: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.
jmcgill said: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.
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",
So, this doesn't happen on x86, Sparc, ARM, MIPS, PPC, 68K, or any other
machine you are likely to be using.
Quantum said:Nick said:Quantum said:Are these equivalent:
char text[];
Good point! I'm passing text[] into a function, however it is still
declared as a pointer.![]()
jmcgill said: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.
What about 0x0?
I used to use that for pointers since, for me, is a address and points
out that you don't set a value to zero but and address.
If now NULL is not guarnteed to be zero on some systems would then if (
!ptr ) work? Since false is 0.
Those of you that does this as professionals, what are the corporate
standard on this? Im guessing NULL.
TiraX said:What about 0x0? I used to use that for pointers since, for me, is a
address and points out that you don't set a value to zero but and
address.
If now NULL is not guarnteed to be zero on some systems would then if (
!ptr ) work? Since false is 0.
Frederick said:The macro, NULL, must be a compile-time integer constant which evaluates to
zero. No more. No less.
Not true. It could be anything as long as it can be converted
to a pointer, resulting in a null pointer.
For example, GCC defines NULL as __nullptr, which is a value
that meets this requirement. This definition does not break
any standards conformance.
Old said:Not true. It could be anything as long as it can be converted
to a pointer, resulting in a null pointer.
For example, GCC defines NULL as __nullptr, which is a value
that meets this requirement. This definition does not break
any standards conformance.
Old said:Not true. It could be anything as long as it can be converted
to a pointer, resulting in a null pointer.
GCC is doing this as an extension to the language.For example, GCC defines NULL as __nullptr, which is a value
that meets this requirement. This definition does not break
any standards conformance.
Clark said:__nullptr in GCC is still a constant integer expression that evaluates
to zero. Were it not, it could not be a null pointer constant.
Ron said:Actually, it isn't. It's technically illegal for NULL to be defined
that way, but unless you are abusing NULL, you'd never know.
int x = __nullptr;
is an error in GCC.
Clark said:GCC *will* give a warning (not an error) for the following program.
Again, this is a program that *any* C++ compiler *must* accept:\
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.