473,385 Members | 1,890 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,385 software developers and data experts.

is '\0' == 0


'\0' is all bits set to zero.

Is all bits zero equal to integer's zero?

--
|
 ___
(-_-)
<¡ä¡ä>¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w ªÅ´ßªª³õ shepjeng.twbbs.org ¢w¢w¢w
¡þ plum.cs.nccu.edu.tw
Nov 13 '05 #1
38 2112

"I wish" <GA************@shepjeng.twbbs.org> wrote in message
news:48********@shepjeng.twbbs.org...

'\0' is all bits set to zero.

Is all bits zero equal to integer's zero?


'\0' is in fact equal to 0. Note that character literals actually
have type int (and so does integral numeric literals if they
can fit in type int).

When you have the escape character in front of an integral
number in string or character literal then that number will be
used as it stands.

Remember that a char is just a integral type as short and long
are.

I don't have a c compiler handy so you might need to fix some
syntax errors in the below.

#include <stdio.h>

int main(void)
{
char a, b;

a = '\0';
b = 0;
if(a == b)
puts("a and b are equal here");

a = '\1';
b = 1;
if(a == b)
puts("Looks like a and b are equal here.");

return 0;
}
Nov 13 '05 #2
Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:
'\0' is in fact equal to 0.


Just for the sake of pedanticness, does this imply that the implementation
must represent '\0' and 0 in the same way?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #3
Christopher Benson-Manica wrote:

Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:
'\0' is in fact equal to 0.


Just for the sake of pedanticness, does this imply that the implementation
must represent '\0' and 0 in the same way?


Just for the sake of pedantry, it's impossible to tell:
You cannot inspect the representation of a numeric literal.
You can store the literal's value into a data object and
inspect *that*, but it's unsafe to conclude anything much
about how the literal itself might be represented.

For example, consider a machine with a "store zero"
machine instruction. The C code `int x = 0;' might
generate something like `stz x' -- and the literal would
have *no* representational image at all!

--
Er*********@sun.com
Nov 13 '05 #4
Eric Sosman wrote:

Christopher Benson-Manica wrote:

Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:
'\0' is in fact equal to 0.
Just for the sake of pedanticness,
does this imply that the implementation
must represent '\0' and 0 in the same way?


Just for the sake of pedantry, it's impossible to tell:
You cannot inspect the representation of a numeric literal.


What is a "numeric literal" ?
You can store the literal's value into a data object and
inspect *that*, but it's unsafe to conclude anything much
about how the literal itself might be represented.


/* I don't know what you mean. */

#include <stdio.h>

int main(void)
{

#if !('\1' & -'\1')
printf("ones complement\n");
#elif -'\1' & '\2'
printf("twos complement\n");
#else
printf("sign magnitude\n");
#endif

return 0;
}
Nov 13 '05 #5
pete wrote:

Eric Sosman wrote:

Christopher Benson-Manica wrote:

Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:

> '\0' is in fact equal to 0.

Just for the sake of pedanticness,
does this imply that the implementation
must represent '\0' and 0 in the same way?


Just for the sake of pedantry, it's impossible to tell:
You cannot inspect the representation of a numeric literal.


What is a "numeric literal" ?
You can store the literal's value into a data object and
inspect *that*, but it's unsafe to conclude anything much
about how the literal itself might be represented.


/* I don't know what you mean. */

#include <stdio.h>

int main(void)
{

#if !('\1' & -'\1')
printf("ones complement\n");
#elif -'\1' & '\2'
printf("twos complement\n");
#else
printf("sign magnitude\n");
#endif

return 0;
}


I don't know what I was thinking with that code.
Nov 13 '05 #6
pete wrote:

Eric Sosman wrote:

Christopher Benson-Manica wrote:

Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:

> '\0' is in fact equal to 0.

Just for the sake of pedanticness,
does this imply that the implementation
must represent '\0' and 0 in the same way?


Just for the sake of pedantry, it's impossible to tell:
You cannot inspect the representation of a numeric literal.


What is a "numeric literal" ?


0, 42, 0x2A, 2.718, '\0', 'X', ...

(The last two may be debatable.)

--
Er*********@sun.com
Nov 13 '05 #7
On Fri, 19 Sep 2003 21:54:20 +0200, "Thomas Stegen"
<ts*****@cis.strath.ac.uk> wrote in comp.lang.c:

"I wish" <GA************@shepjeng.twbbs.org> wrote in message
news:48********@shepjeng.twbbs.org...

'\0' is all bits set to zero.

Is all bits zero equal to integer's zero?


'\0' is in fact equal to 0. Note that character literals actually
have type int (and so does integral numeric literals if they
can fit in type int).

When you have the escape character in front of an integral
number in string or character literal then that number will be
used as it stands.


I hope you didn't really mean literally what you said in the paragraph
above. When you have an escape character in front of certain
specifically formatted numeric escape sequences, the entire escape
sequence is replaced with a numeric value.

But "\9" is undefined behavior, not a character string containing a
character with the binary value 9 followed by a null character.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #8

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:86********************************@4ax.com...
On Fri, 19 Sep 2003 21:54:20 +0200, "Thomas Stegen"
<ts*****@cis.strath.ac.uk> wrote in comp.lang.c:
When you have the escape character in front of an integral
number in string or character literal then that number will be
used as it stands.


I hope you didn't really mean literally what you said in the paragraph
above.


Sort of not literally. I was being a bit informal. Which is
not a good idea around here ;)

Thank you for your clarification.

--
Thomas.
Nov 13 '05 #9
Eric Sosman wrote:

pete wrote:

Eric Sosman wrote:

Christopher Benson-Manica wrote:
>
> Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:
>
> > '\0' is in fact equal to 0.
>
> Just for the sake of pedanticness,
> does this imply that the implementation
> must represent '\0' and 0 in the same way?

Just for the sake of pedantry, it's impossible to tell:
You cannot inspect the representation of a numeric literal.


What is a "numeric literal" ?


0, 42, 0x2A, 2.718, '\0', 'X', ...

(The last two may be debatable.)


OK, but now fixing that screwy post that made,
one may form expressions wich only have
constants and bitwise operators.

#if !(1 & -1)
printf("ones complement\n");
#elif -1 & 2
printf("twos complement\n");
#else
printf("sign magnitude\n");
#endif

--
pete
Nov 13 '05 #10
Thomas Stegen wrote:

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:86********************************@4ax.com...
On Fri, 19 Sep 2003 21:54:20 +0200, "Thomas Stegen"
<ts*****@cis.strath.ac.uk> wrote in comp.lang.c:
When you have the escape character in front of an integral
number in string or character literal then that number will be
used as it stands.


I hope you didn't really mean
literally what you said in the paragraph
above.


Sort of not literally. I was being a bit informal. Which is
not a good idea around here ;)

Thank you for your clarification.


I posted code with that same mistake in a
"Re: is '\0' == 0" post, just yesterday.

--
pete
Nov 13 '05 #11
"I wish" <GA************@shepjeng.twbbs.org> wrote in message
news:48********@shepjeng.twbbs.org...

'\0' is all bits set to zero.

Is all bits zero equal to integer's zero?


Yes (except that 'char' and 'int' might have different
sizes). The \ form is used to express a specific
numeric value in a char literal. e.g. '0' specifies
the value for whatever the host character set
used for the text digit zero.

So

char c = '\0';

is equivalent to

char c = 0;
On an ASCII system:

char c = '\65';

is equivalent to:

char c = 'A';

-Mike
Nov 13 '05 #12
The scape secuence '\0' is equal to word NULL and same that integer 0.

Bus is best to use each thing for which it is for avoiding to embroil
and to be confused
Nov 13 '05 #13
kl*****@hotmail.com (Klannet) wrote:
The scape secuence '\0' is equal to word NULL and same that integer 0.


Nonsense. NULL is a null-pointer constant which may or may not be equal
to 0. Though, a 0 in pointer contexts is converted to NULL.

Please read the FAQ, section 5.

Regards

Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #14
Mike Wahler wrote:

"I wish" <GA************@shepjeng.twbbs.org> wrote in message
news:48********@shepjeng.twbbs.org...

'\0' is all bits set to zero.

Is all bits zero equal to integer's zero?


Yes (except that 'char' and 'int' might have different
sizes). The \ form is used to express a specific
numeric value in a char literal. e.g. '0' specifies
the value for whatever the host character set
used for the text digit zero.

So

char c = '\0';

is equivalent to

char c = 0;

On an ASCII system:

char c = '\65';

is equivalent to:

char c = 'A';

No. The '\' introduces an octal constant. 'A' == '\101' in ASCII.

--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #15
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<eu********************************@4ax.com>. ..
kl*****@hotmail.com (Klannet) wrote:
The scape secuence '\0' is equal to word NULL and same that integer 0.

Nonsense.


In what way? 0 and '\0' are semantically identical in C and both must
compare equal (==) to NULL when used as integer constants...
NULL is a null-pointer constant which may or may not be equal
to 0.
The expression (NULL == 0) is always true.
Though, a 0 in pointer contexts is converted to NULL.


No, a constant integer expression with value 0 is converted to a null
pointer of suitable type. Whist the resulting pointer must compare
equal to NULL, it need not have the same type or representation as
NULL. I.e., the following may print a non zero value...

T *a = 0;
T *b = NULL;
printf("%d\n", memcmp(&a, &b, sizeof a));

--
Peter
Nov 13 '05 #16
ai***@acay.com.au (Peter Nilsson) wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<eu********************************@4ax.com>. ..
kl*****@hotmail.com (Klannet) wrote:
>The scape secuence '\0' is equal to word NULL and same that integer 0.
>
Nonsense.


In what way? 0 and '\0' are semantically identical in C and both must
compare equal (==) to NULL when used as integer constants...


Right, because they get converted to a null pointer in pointer contexts.
NULL is a null-pointer constant which may or may not be equal
to 0.

Hm, I used improper wording here, see end of reply.

The expression (NULL == 0) is always true.
Right, because:
- your implementation defines NULL as 0 (trivial comparision then)
or
- your implementation defines NULL as (sometype *)0, which results in
the above example in 0 being converted to a null pointer-to-sometype,
because 0 is used in a pointer context.

Note that when you pass 0 as an argument to a function, the pointer
context may not be obvious, so an explicit cast may be required (eg. in
a call to a variadic function).

Consider:

printf( "NULL looks like: %p", 0 ); /* UB! */

(See c.l.c-faq section 5.2)
Though, a 0 in pointer contexts is converted to NULL.


No, a constant integer expression with value 0 is converted to a null
pointer of suitable type. Whist the resulting pointer must compare
equal to NULL, it need not have the same type or representation as
NULL. I.e., the following may print a non zero value...

T *a = 0;
T *b = NULL;
printf("%d\n", memcmp(&a, &b, sizeof a));


Agreed, Again, I used improper wording.

Regards,

Irrwahn

--
My other computer is a abacus.
Nov 13 '05 #17
On 21 Sep 2003 19:38:18 -0700, in comp.lang.c , ai***@acay.com.au
(Peter Nilsson) wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<eu********************************@4ax.com>. ..
NULL is a null-pointer constant which may or may not be equal
to 0.


The expression (NULL == 0) is always true.


This is true, but not contradictory to Irrwahn's statement. Think
about it.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #18
In <63**************************@posting.google.com > ai***@acay.com.au (Peter Nilsson) writes:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<eu********************************@4ax.com>. ..
kl*****@hotmail.com (Klannet) wrote:
>The scape secuence '\0' is equal to word NULL and same that integer 0.


Nonsense.


In what way? 0 and '\0' are semantically identical in C and both must
compare equal (==) to NULL when used as integer constants...
NULL is a null-pointer constant which may or may not be equal
to 0.


The expression (NULL == 0) is always true.


Klannet implied that the three forms can be used interchangeably,
even if it's not a good idea. Read the whole post.

The truth is that NULL cannot be used interchangeably with the other two
forms, even if the expression (NULL == 0) is always true.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #19
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<4m********************************@4ax.com>. ..
ai***@acay.com.au (Peter Nilsson) wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<eu********************************@4ax.com>. ..
kl*****@hotmail.com (Klannet) wrote:

>The scape secuence '\0' is equal to word NULL and same that integer 0.
>

Nonsense.
In what way? 0 and '\0' are semantically identical in C and both must
compare equal (==) to NULL when used as integer constants...


Right, because they get converted to a null pointer in pointer contexts.
NULL is a null-pointer constant which may or may not be equal
to 0.


Hm, I used improper wording here, see end of reply.

The expression (NULL == 0) is always true.


Right, because:
- your implementation defines NULL as 0 (trivial comparision then)
or
- your implementation defines NULL as (sometype *)0, which results in
the above example in 0 being converted to a null pointer-to-sometype,
because 0 is used in a pointer context.

Note that when you pass 0 as an argument to a function, the pointer
context may not be obvious, so an explicit cast may be required (eg. in
a call to a variadic function).

Consider:

printf( "NULL looks like: %p", 0 ); /* UB! */

(See c.l.c-faq section 5.2)


Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB? Or should NULL always be
defined as ( some_type * ) 0?

MOreover if I write some type,can it literly be "any_type"?
Though, a 0 in pointer contexts is converted to NULL.


No, a constant integer expression with value 0 is converted to a null
pointer of suitable type. Whist the resulting pointer must compare
equal to NULL, it need not have the same type or representation as
NULL. I.e., the following may print a non zero value...

T *a = 0;
T *b = NULL;
printf("%d\n", memcmp(&a, &b, sizeof a));


Agreed, Again, I used improper wording.

Regards,

Irrwahn

Nov 13 '05 #20
j

"Minti" <mi************@yahoo.com> wrote in message
news:e8**************************@posting.google.c om...
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<4m********************************@4ax.com>. ..
ai***@acay.com.au (Peter Nilsson) wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in message news:<eu********************************@4ax.com>. ..> kl*****@hotmail.com (Klannet) wrote:
>
> >The scape secuence '\0' is equal to word NULL and same that integer 0.> >
>
> Nonsense.

In what way? 0 and '\0' are semantically identical in C and both must
compare equal (==) to NULL when used as integer constants...


Right, because they get converted to a null pointer in pointer contexts.

> NULL is a null-pointer constant which may or may not be equal
> to 0.


Hm, I used improper wording here, see end of reply.

The expression (NULL == 0) is always true.


Right, because:
- your implementation defines NULL as 0 (trivial comparision then)
or
- your implementation defines NULL as (sometype *)0, which results in
the above example in 0 being converted to a null pointer-to-sometype,
because 0 is used in a pointer context.

Note that when you pass 0 as an argument to a function, the pointer
context may not be obvious, so an explicit cast may be required (eg. in
a call to a variadic function).

Consider:

printf( "NULL looks like: %p", 0 ); /* UB! */

(See c.l.c-faq section 5.2)


Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB?


Yes. Since %p requires a corresponding value casted to pointer to void.
Or should NULL always be
defined as ( some_type * ) 0?

I don't think it can ever be defined as ``(some_type *)0''
The standard says the following about what a null pointer constant is:

``An integer constant expression with the value 0, or such an expression
cast to type
void *, is called a null pointer constant.55)''

So it can either be:
a) The value 0
b) or such an expression casted to void *
MOreover if I write some type,can it literly be "any_type"?

> Though, a 0 in pointer contexts is converted to NULL.

No, a constant integer expression with value 0 is converted to a null
pointer of suitable type. Whist the resulting pointer must compare
equal to NULL, it need not have the same type or representation as
NULL. I.e., the following may print a non zero value...

T *a = 0;
T *b = NULL;
printf("%d\n", memcmp(&a, &b, sizeof a));


Agreed, Again, I used improper wording.

Regards,

Irrwahn

Nov 13 '05 #21
"j" <ja*******@bellsouth.net> wrote:

"Minti" <mi************@yahoo.com> wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote:
>
> Consider:
>
> printf( "NULL looks like: %p", 0 ); /* UB! */
>
> (See c.l.c-faq section 5.2)


Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB?


Yes. Since %p requires a corresponding value casted to pointer to void.
Or should NULL always be
defined as ( some_type * ) 0?


I don't think it can ever be defined as ``(some_type *)0''
The standard says the following about what a null pointer constant is:

``An integer constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant.55)''

So it can either be:
a) The value 0
b) or such an expression casted to void *

Yes, you are right, this of course how the implementation should define
the null pointer constant.

The quote of the relevant C&V from ISO/IEC 9899:1999 is:

6.3.2.3 Pointers

[...]
3 An integer constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant.55) If a null
pointer constant is converted to a pointer type, the resulting
pointer, called a null pointer, is guaranteed to compare unequal to a
pointer to any object or function.

4 Conversion of a null pointer to another pointer type yields a null
pointer of that type. Any two null pointers shall compare equal.

[...]
55) The macro NULL is defined in <stddef.h> (and other headers) as a
null pointer constant; [...]
Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #22
mi************@yahoo.com (Minti) writes:
Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB? Or should NULL always be
defined as ( some_type * ) 0?
Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.
MOreover if I write some type,can it literly be "any_type"?


Not certain what you're asking here...

-Micah
Nov 13 '05 #23
mi************@yahoo.com (Minti) writes:
Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB? Or should NULL always be
defined as ( some_type * ) 0?
Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.
MOreover if I write some type,can it literly be "any_type"?


Not certain what you're asking here...

-Micah
Nov 13 '05 #24
In <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
mi************@yahoo.com (Minti) writes:
Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB? Or should NULL always be
defined as ( some_type * ) 0?


Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.


Nope, it must be cast to the pointer type expected by the callee,
which is this particular example happens to be void *.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #25
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<PO***************@newsread4.news.pas.earthli nk.net>...
"I wish" <GA************@shepjeng.twbbs.org> wrote in message
news:48********@shepjeng.twbbs.org...

'\0' is all bits set to zero.

Is all bits zero equal to integer's zero?


Yes (except that 'char' and 'int' might have different
sizes).


My copy of C99 is not available to me at the moment, but I am relatively
certain that in C, (sizeof('\0') == sizeof(0)) is always true.

[ The expression may not be true in C++, but this is comp.lang.c. ]

The C-faq provides a pretty detailed discussion of the differences between
NUL, NULL, 0, '\0', and (void *)0. I would recommend the original poster
give it a read. http://www.eskimo.com/~scs/C-faq/top.html

"The C-faq: Purity Guaranteed"

-- James
Nov 13 '05 #26
Da*****@cern.ch (Dan Pop) writes:
In <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
mi************@yahoo.com (Minti) writes:
Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB? Or should NULL always be
defined as ( some_type * ) 0?


Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.


Nope, it must be cast to the pointer type expected by the callee,
which is this particular example happens to be void *.


Er, right! Thanks for the clarification ;-)

-Micah
Nov 13 '05 #27
jx*@despammed.com (James Hu) writes:
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<PO***************@newsread4.news.pas.earthli nk.net>...
"I wish" <GA************@shepjeng.twbbs.org> wrote in message
news:48********@shepjeng.twbbs.org...

'\0' is all bits set to zero.

Is all bits zero equal to integer's zero?
Yes (except that 'char' and 'int' might have different
sizes).


My copy of C99 is not available to me at the moment, but I am relatively
certain that in C, (sizeof('\0') == sizeof(0)) is always true.


Yup. Character literals are always of type int in C.
[ The expression may not be true in C++, but this is comp.lang.c. ]


(Yeah, I think someone was thinking in C++...)

Also, '\0' is *not* necessarily "all bits set to zero"
(if int has padding bits)...

-Micah
Nov 13 '05 #28
Micah Cowan <mi***@cowan.name> wrote in message news:<m3************@localhost.localdomain>...
mi************@yahoo.com (Minti) writes:
Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB? Or should NULL always be
defined as ( some_type * ) 0?


Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.
MOreover if I write some type,can it literly be "any_type"?


Not certain what you're asking here...


Well, Firstly thanks, what I was asking was that can we declare NULL
pointer to be a 0 casted to a pointer to *any* type.

like (char*) 0 and (int*) 0

That question seems to be answered, it has to be of the type that is
expected by the callee.

But I have one question now say I have

void foo()
{

if ( (char*) 0 == (void*) 0 )
printf("Equality proved\n");

else
printf("Equality Disaproved\n");

}

What would be the output of the above?

Thanks again

--
Imanpreet Singh Arora
imanpreet_arora AT yahoo DOT co DOT in
Nov 13 '05 #29
mi************@yahoo.com (Minti) wrote:
Well, Firstly thanks, what I was asking was that can we declare NULL
pointer to be a 0 casted to a pointer to *any* type.

like (char*) 0 and (int*) 0

That question seems to be answered, it has to be of the type that is
expected by the callee.

But I have one question now say I have

void foo()
{

if ( (char*) 0 == (void*) 0 )
printf("Equality proved\n");

else
printf("Equality Disaproved\n");

}

What would be the output of the above?


As I already quoted in another subthread, ISO/IEC 9899:1999 says:

6.3.2.3 Pointers

[...]

4 Conversion of a null pointer to another pointer type yields a null
pointer of that type. Any two null pointers shall compare equal.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Note that the _representation_ of two null pointers may differ, though
they _compare_ equal.

Regards

Irrwahn
--
Computer: a million morons working at the speed of light.
Nov 13 '05 #30
"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:qq********************************@4ax.com...
mi************@yahoo.com (Minti) wrote:
Well, Firstly thanks, what I was asking was that can we declare NULL
pointer to be a 0 casted to a pointer to *any* type.

like (char*) 0 and (int*) 0

That question seems to be answered, it has to be of the type that is
expected by the callee.

But I have one question now say I have

void foo()
{

if ( (char*) 0 == (void*) 0 )
printf("Equality proved\n");

else
printf("Equality Disaproved\n");

}

What would be the output of the above?


As I already quoted in another subthread, ISO/IEC 9899:1999 says:

6.3.2.3 Pointers

[...]

4 Conversion of a null pointer to another pointer type yields a null
pointer of that type. Any two null pointers shall compare equal.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


That alone isn't enough. The pointers being compared need to be the same
type, which is given by 6.5.9p6:

"...If one operand is a pointer to an object or incomplete type and
the other is a pointer to a qualified or unqualified version of
void, the former is converted to the type of the latter."

--
Peter
Nov 13 '05 #31
On 26 Sep 2003 19:00:36 -0700, mi************@yahoo.com (Minti) wrote:
Micah Cowan <mi***@cowan.name> wrote in message news:<m3************@localhost.localdomain>...
mi************@yahoo.com (Minti) writes:
> Thanks, but I have one query. Say my implementaion defines NULL to be
> 0, Now if we have a variadic funciton I pass NULL not 0. Like
>
> printf("NULL looks like: %p\n", NULL);
>
> Is it possible for above to generate UB? Or should NULL always be
> defined as ( some_type * ) 0?
Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.
> MOreover if I write some type,can it literly be "any_type"?


Not certain what you're asking here...


Well, Firstly thanks, what I was asking was that can we declare NULL
pointer to be a 0 casted to a pointer to *any* type.

like (char*) 0 and (int*) 0

That question seems to be answered, it has to be of the type that is
expected by the callee.


No, NULL has to be defined as either an integer constant expression
equal to 0 or such an expression cast to pointer to void.

When past as an argument to a function with a prototype in scope,
it will be converted automatically to the pointer type of the
corresponding parameter. That is why the same NULL can be passed to
both free (which expects a void*) and strtol (which expects a char**).

When passed to a variadic function, it needs to be (manually) cast
to the proper type since the compiler doesn't know what that is.

But I have one question now say I have

void foo()
{

if ( (char*) 0 == (void*) 0 )
printf("Equality proved\n");

else
printf("Equality Disaproved\n");

}

No different than the hundreds of examples where people compare the
return from fgets or strstr to NULL.
<<Remove the del for email>>
Nov 13 '05 #32
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:qq********************************@4ax.com.. .
mi************@yahoo.com (Minti) wrote:
>Well, Firstly thanks, what I was asking was that can we declare NULL
>pointer to be a 0 casted to a pointer to *any* type.
>
>like (char*) 0 and (int*) 0
>
>That question seems to be answered, it has to be of the type that is
>expected by the callee.
>
>But I have one question now say I have
>
>void foo()
>{
>
> if ( (char*) 0 == (void*) 0 )
> printf("Equality proved\n");
>
> else
> printf("Equality Disaproved\n");
>
>}
>
>What would be the output of the above?
>


As I already quoted in another subthread, ISO/IEC 9899:1999 says:

6.3.2.3 Pointers

[...]

4 Conversion of a null pointer to another pointer type yields a null
pointer of that type. Any two null pointers shall compare equal.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


That alone isn't enough. The pointers being compared need to be the same
type, which is given by 6.5.9p6:

"...If one operand is a pointer to an object or incomplete type and
the other is a pointer to a qualified or unqualified version of
void, the former is converted to the type of the latter."


Not quite, please quote all relevant verses:

ISO/IEC 9899:1999 6.5.6

[...]

5 Otherwise, at least one operand is a pointer. If one operand is a
pointer and the other is a null pointer constant, the null pointer
constant is converted to the type of the pointer. If one operand is
a pointer to an object or incomplete type and the other is a pointer
to a qualified or unqualified version of void, the former is
converted to the type of the latter.

6 Two pointers compare equal if and only if both are null pointers,
both are pointers to the same object (including a pointer to an
object and a subobject at its beginning) or function, both are
pointers to one past the last element of the same array object, or
one is a pointer to one past the end of one array object and the
other is a pointer to the start of a different array object that
happens to immediately follow the first array object in the address
space.91)

Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #33
Irrwahn Grausewitz <ir*****************@freenet.de> wrote:

ISO/IEC 9899:1999 6.5.6

Typo here, should be 6.5.9, of course...
Nov 13 '05 #34
"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:bv********************************@4ax.com...
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:qq********************************@4ax.com.. .
mi************@yahoo.com (Minti) wrote:

>Well, Firstly thanks, what I was asking was that can we declare NULL
>pointer to be a 0 casted to a pointer to *any* type.
>
>like (char*) 0 and (int*) 0
>
>That question seems to be answered, it has to be of the type that is
>expected by the callee.
>
>But I have one question now say I have
>
>void foo()
>{
>
> if ( (char*) 0 == (void*) 0 )
> printf("Equality proved\n");
>
> else
> printf("Equality Disaproved\n");
>
>}
>
>What would be the output of the above?
>

As I already quoted in another subthread, ISO/IEC 9899:1999 says:

6.3.2.3 Pointers

[...]

4 Conversion of a null pointer to another pointer type yields a null
pointer of that type. Any two null pointers shall compare equal.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That alone isn't enough. The pointers being compared need to be the same
type, which is given by 6.5.9p6:

"...If one operand is a pointer to an object or incomplete type and
the other is a pointer to a qualified or unqualified version of
void, the former is converted to the type of the latter."


Not quite, please quote all relevant verses:

ISO/IEC 9899:1999 6.5.6


ITYM 6.5.9
[...]

5 Otherwise, at least one operand is a pointer. If one operand is a ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pointer and the other is a null pointer constant, the null pointer
constant is converted to the type of the pointer. If one operand is
a pointer to an object or incomplete type and the other is a pointer
to a qualified or unqualified version of void, the former is
converted to the type of the latter.
How is that relevant to the case where both operands are *already* pointers?

6 Two pointers compare equal if and only if both are null pointers,

<snip>

That is notwithstanding your original quote from 6.3.2.3p4. If it was, then
we would need to lodge a defect report.

--
Peter
Nov 13 '05 #35
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:bv********************************@4ax.com.. .
"Peter Nilsson" <ai***@acay.com.au> wrote:
>"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
>>
>> 6.3.2.3 Pointers
>>
>> [...]
>>
>> 4 Conversion of a null pointer to another pointer type yields a null
>> pointer of that type. Any two null pointers shall compare equal.
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>That alone isn't enough. The pointers being compared need to be the same
>type, which is given by 6.5.9p6: ^^ >
> "...If one operand is a pointer to an object or incomplete type and
> the other is a pointer to a qualified or unqualified version of
> void, the former is converted to the type of the latter."
Not quite, please quote all relevant verses:

ISO/IEC 9899:1999 6.5.6


ITYM 6.5.9


Yes, I made a typo.
[...]

5 Otherwise, at least one operand is a pointer. If one operand is a

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pointer and the other is a null pointer constant, the null pointer
constant is converted to the type of the pointer. If one operand is
a pointer to an object or incomplete type and the other is a pointer
to a qualified or unqualified version of void, the former is
converted to the type of the latter.


How is that relevant to the case where both operands are *already* pointers?


I just did the full quote because you were referring to 6.5.9p6 but
actually quoted (part of) 6.5.9#5.
I think we're quit now that I made the stupid 6.5.6-typo ... :)

6 Two pointers compare equal if and only if both are null pointers,

<snip>

That is notwithstanding your original quote from 6.3.2.3p4. If it was, then
we would need to lodge a defect report.


Absolutely agreed. It's just here, because it's the Real Paragraph
Six(TM). ;-)

Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #36
mi************@yahoo.com (Minti) writes:
Micah Cowan <mi***@cowan.name> wrote in message news:<m3************@localhost.localdomain>...
mi************@yahoo.com (Minti) writes:
Thanks, but I have one query. Say my implementaion defines NULL to be
0, Now if we have a variadic funciton I pass NULL not 0. Like

printf("NULL looks like: %p\n", NULL);

Is it possible for above to generate UB? Or should NULL always be
defined as ( some_type * ) 0?
Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.
MOreover if I write some type,can it literly be "any_type"?


Not certain what you're asking here...


Well, Firstly thanks, what I was asking was that can we declare NULL
pointer to be a 0 casted to a pointer to *any* type.

like (char*) 0 and (int*) 0

That question seems to be answered, it has to be of the type that is
expected by the callee.


No: You can't possibly declare NULL to be cast to the type
expected by the calleee. But you must cast it before passing it
through a variadic or unprototyped function, to ensure the proper
representation.

NULL must be defined as either 0 or (void*)0.
But I have one question now say I have

void foo()
{

if ( (char*) 0 == (void*) 0 )
printf("Equality proved\n");

else
printf("Equality Disaproved\n");

}

What would be the output of the above?


Equality proved.

(char*)0 would be converted to (void*)0 before the comparison.

-Micah
Nov 13 '05 #37
"Micah Cowan" <mi***@cowan.name> wrote in message
news:m3************@localhost.localdomain...
mi************@yahoo.com (Minti) writes:
Micah Cowan <mi***@cowan.name> wrote in message news:<m3************@localhost.localdomain>...
mi************@yahoo.com (Minti) writes:

> Thanks, but I have one query. Say my implementaion defines NULL to be > 0, Now if we have a variadic funciton I pass NULL not 0. Like
>
> printf("NULL looks like: %p\n", NULL);
>
> Is it possible for above to generate UB? Or should NULL always be
> defined as ( some_type * ) 0?

Yes! This is why it is recommended that in variadic or
non-prototyped functions, NULL be cast to void* first to be safe.

> MOreover if I write some type,can it literly be "any_type"?

Not certain what you're asking here...


Well, Firstly thanks, what I was asking was that can we declare NULL
pointer to be a 0 casted to a pointer to *any* type.

like (char*) 0 and (int*) 0

That question seems to be answered, it has to be of the type that is
expected by the callee.


No: You can't possibly declare NULL to be cast to the type
expected by the calleee. But you must cast it before passing it
through a variadic or unprototyped function, to ensure the proper
representation.

NULL must be defined as either 0 or (void*)0.
But I have one question now say I have

void foo()
{

if ( (char*) 0 == (void*) 0 )
printf("Equality proved\n");

else
printf("Equality Disaproved\n");

}

What would be the output of the above?


Equality proved.

(char*)0 would be converted to (void*)0 before the comparison.


No. Technically, (void *) 0 is converted to a (char *).

6.9.5p5 "...If one operand is a pointer and the other is a null pointer
constant, the null pointer constant is converted to the type of the pointer.
...."

--
Peter
Nov 13 '05 #38
"Peter Nilsson" <ai***@acay.com.au> writes:
"Micah Cowan" <mi***@cowan.name> wrote in message
news:m3************@localhost.localdomain...
mi************@yahoo.com (Minti) writes:
But I have one question now say I have

void foo()
{

if ( (char*) 0 == (void*) 0 )
printf("Equality proved\n");

else
printf("Equality Disaproved\n");

}

What would be the output of the above?


Equality proved.

(char*)0 would be converted to (void*)0 before the comparison.


No. Technically, (void *) 0 is converted to a (char *).

6.9.5p5 "...If one operand is a pointer and the other is a null pointer
constant, the null pointer constant is converted to the type of the pointer.
..."


Erp, you're right. I think I confused it with the very next sentence:

If one operand is a pointer to an object or incomplete type and
the other is a pointer to a qualified or unqualified version of
void, the former is converted to the type of the latter.

In the case of NULL being defined as (void*)0, *both* sentences
would apply. Luckily, they have the same semantic meaning in this
case, so you could pick one and it'd apply to the other under the
"as if" rule...

-Micah
Nov 13 '05 #39

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

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.