L
lak
What is the difference b/w '\0' and NULL?
In which case It is useful?
In which case It is useful?
lak said:What is the difference b/w '\0' and NULL?
In which case It is useful?
santosh said:The first is the representation for the null character, i.e., a
character with value zero. It is used as a string terminator in C.
It is actually an "escape sequence" with an octal zero.
The second is a macro that resolves to a null pointer value. In C source
code a literal zero is also converted into a null pointer constant when
it occurs in a pointer context.
The first expression is of type int while the second is of a pointer
type.
Richard said:Subtly wrong. NULL is a null pointer constant, which is either an
integer constant with value zero, or such a constant cast to void *. In
the second case it has pointer type, but in the first case it has
integer type. In fact, confusingly, '\0' is a legal (but unwise)
spelling for a null pointer constant.
While this is valid:
void *ptr = '\0';
the following will most likely fail (or at least give a warning):
int i = NULL;
Yes, the standard allows NULL to be an "integer constant with value
zero" (such as "#define NULL 0"), but it is more likely defined as
a value cast to void * (as in "#define NULL ((void *)0)"), simply
because it's "safer".
(In fact, it's probably required in an
implementation where int and void* are represented differently.
Consider the consequences of passing VOID to a varadic function.)
(In fact, it's probably required in an implementation
where int and void* are represented differently.
Consider the
consequences of passing VOID to a varadic function.)
Flash said:Kenneth Brody wrote, On 26/11/07 18:19: [...]Yes, the standard allows NULL to be an "integer constant with value
zero" (such as "#define NULL 0"), but it is more likely defined as
a value cast to void * (as in "#define NULL ((void *)0)"), simply
because it's "safer".
It definitely makes sense for this to be done, although I'm sure one of
the implementations I've used in the past few years did not do it.
(In fact, it's probably required in an
implementation where int and void* are represented differently.
No, this is definitely not true. A conforming implementation can use
"#define NULL 0" irrespective of the relative sizes and representations.
Consider the consequences of passing VOID to a varadic function.)
Assuming you mean passing a null pointer to a varidac function, yes that
is a problem and one of the few instances where you need a cast.
Would you really need to pass "(void *)NULL"? Ewww.
Yes, the standard as-written probably requires it. I wonder if they
meant that to be the case?
Kenneth said:Would you really need to pass "(void *)NULL"? Ewww.
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.