PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Difference between '\0' and NULL
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Difference between '\0' and NULL

Réponse
 
LinkBack Outils de la discussion
Vieux 26/11/2007, 10h10   #1
lak
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Difference between '\0' and NULL

What is the difference b/w '\0' and NULL?
In which case It is useful?
  Réponse avec citation
Vieux 26/11/2007, 10h38   #2
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

In article
<fda785d2-d67d-420a-a6fc-cd6c472ff723@d27g2000prf.googlegroups.com>,
lak <lakindia89@gmail.com> wrote on Monday 26 Nov 2007 2:40 pm:

> What is the difference b/w '\0' and NULL?


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.

> In which case It is useful?


Use '\0' to terminate strings and NULL to initialise pointers and set
them to a "safe" value after they have been used.

Please browse the comp.lang.c FAQ:

<http://c-faq.com/>

  Réponse avec citation
Vieux 26/11/2007, 10h53   #3
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

santosh <santosh.k83@gmail.com> wrote:

> lak <lakindia89@gmail.com> wrote on Monday 26 Nov 2007 2:40 pm:
>
> > What is the difference b/w '\0' and NULL?

>
> 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.


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.

Richard
  Réponse avec citation
Vieux 26/11/2007, 19h19   #4
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

Richard Bos wrote:
>
> santosh <santosh.k83@gmail.com> wrote:
>
> > lak <lakindia89@gmail.com> wrote on Monday 26 Nov 2007 2:40 pm:
> >
> > > What is the difference b/w '\0' and NULL?

> >
> > 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.

>
> 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.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>

  Réponse avec citation
Vieux 26/11/2007, 20h54   #5
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

Kenneth Brody wrote, On 26/11/07 18:19:
> Richard Bos wrote:
>> santosh <santosh.k83@gmail.com> wrote:
>>
>>> lak <lakindia89@gmail.com> wrote on Monday 26 Nov 2007 2:40 pm:
>>>
>>>> What is the difference b/w '\0' and NULL?
>>> 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.

>> 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".


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.
--
Flash Gordon
  Réponse avec citation
Vieux 26/11/2007, 21h03   #6
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

On Mon, 26 Nov 2007 13:19:34 -0500, Kenneth Brody wrote:
> (In fact, it's probably required in an implementation
> where int and void* are represented differently.


It's not.

> Consider the
> consequences of passing VOID to a varadic function.)


Passing NULL to a variadic function is a bad idea, not only for that
reason, but also because the function probably expects a specific pointer
type other than void *.
  Réponse avec citation
Vieux 26/11/2007, 21h40   #7
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

Flash Gordon wrote:
>
> 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.)


D'oh... s/VOID/NULL/

> 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 J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>


  Réponse avec citation
Vieux 26/11/2007, 22h34   #8
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

Kenneth Brody wrote, On 26/11/07 20:40:
> Flash Gordon wrote:
>> Kenneth Brody wrote, On 26/11/07 18:19:


<snip possible NULL definitions>

>> 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?


I might pass (void*)0 instead. I know someone who provides a macro
called NP, but the code in question pre-dates when you could rely on
having an ANSI C compiler.
--
Flash Gordon
  Réponse avec citation
Vieux 27/11/2007, 11h12   #9
Spoon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Difference between '\0' and NULL

Kenneth Brody wrote:

> Would you really need to pass "(void *)NULL"? Ewww.


cf. http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=null

specifically http://c-faq.com/null/nullreq.html
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h22.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15395 seconds with 17 queries