Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Assigning NULL to a variable (not pointer)

Reply
Thread Tools

Assigning NULL to a variable (not pointer)

 
 
Frederick Gotham
Guest
Posts: n/a
 
      10-16-2006

Somebody posted:

> I thought I remembered that in some systems that NULL is not '\0'.



It's perfectly acceptable that NULL would not be defined as the following
on a conforming implementation:

#define NULL '\0'

Here are some other legal definitions of it:

#define NULL 0
#define NULL (0)
#define NULL (5-5)
#define NULL (27%4-3)


> I know that in the majority of systems NULL can equate to '\0'



NULL _must_ compare equal to '\0'.

if (NULL != '\0') cout << "This is a K++ compiler.";


> , but I thought there were exceptions and did not what to make any
> assumptions.



Make those assumptions, because there are no exceptions.


> Not only that, I thought that the definition of NULL was (at least in
> some instances): ((void *)0). If one attempted to assign that to an
> integer, you would at least get a casting error.


Try to compile the following with a C++ compiler.

#define NULL ((void*)0)

int main()
{
int *p = 0;
}

Your compiler should explain to you why NULL is _never_ defined in that
way.

> If either the above statements are true, I would think that doing
> something like "int var = NULL;" would generally be a bad idea.



Well, neither of them _are_ true, so your every implementation should have
no problem with:

int var = NULL;


> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
> the value '\0' by means of habitual conversions, nothing related with
> NULL itself.



The character literal, '\0' _always_ evaluates to integer zero. This is how
we can write such portable code as:

void strcpy(char *to, char const *from)
{
while (*to++ = *from++);
}

, therefore it's _not_ just habitual conversion.

NULL must be an integer compile-time constant which evalutes to zero. '\0'
must also be zero.

Therefore, NULL must compare equal to '\0', and furthermore, an implementor
may define NULL as '\0'.

--

Frederick Gotham
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      10-16-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> First, again thank you for your posts.
>
> I believe I got the point about NULL.
>
> Now I ask a favor. I made the mistake of using my GMAIL account to
> setup my Google Groups account and the bloody thing dumped my email
> address in the postings. I have since taken care of the issue for my
> posting, but the responses posted still contain my email. Could you
> please delete your postings so that the email sniffers won't find me?
> Thank you in advance.


The majority of people here don't use GG and can't delete the postings.
In fact, none of the people responding in this thread besides you use
it. Some newsreaders are able to issue cancel messages, those might or
might not help your situation.





Brian
 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      10-16-2006
Frederick Gotham posted:


> Try to compile the following with a C++ compiler.
>
> #define NULL ((void*)0)
>
> int main()
> {
> int *p = 0;
> }



int *p = NULL;

--

Frederick Gotham
 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-16-2006
Frederick Gotham wrote:

>> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
>> the value '\0' by means of habitual conversions, nothing related with
>> NULL itself.

>
> The character literal, '\0' _always_ evaluates to integer zero.


Try this:
cout << '\0' << '\n';
cout << 0 << '\n';

--
Salu2
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-16-2006
Julián Albo wrote:
> Frederick Gotham wrote:
>
>>> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
>>> the value '\0' by means of habitual conversions, nothing related with
>>> NULL itself.

>> The character literal, '\0' _always_ evaluates to integer zero.

>
> Try this:
> cout << '\0' << '\n';
> cout << 0 << '\n';
>

Yeah so what? '\0' is an integer zero.
ostream:perator<< however distinguishes some
integer types (e.g., char) from others (like int).
The same is true for char* versus void*.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-16-2006
=?ISO-8859-15?Q?Juli=E1n?= Albo posted:

> Try this:
> cout << '\0' << '\n';



Also try it's equivalent:

cout << (char)0 << '\n';

Indeed, std:stream might define certain overloads for operator<<, but that
doesn't make "char" any less of an integer type, nor its zero value any less
of an integer zero value.

--

Frederick Gotham
 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-16-2006
Ron Natalie wrote:

>>>> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
>>>> the value '\0' by means of habitual conversions, nothing related with
>>>> NULL itself.
>>> The character literal, '\0' _always_ evaluates to integer zero.

>>
>> Try this:
>> cout << '\0' << '\n';
>> cout << 0 << '\n';
>>

> Yeah so what? '\0' is an integer zero.


Is an integer type constant, but does not evaluate to nothing but char 0 in
this case.

My first paragraph was confuse, certainly. The point is that even if NULL is
defined as '\0' is not intended to be used like that char literal, but in
contexts where the concrete integer type does not matter. And if is not
defined that way, the char also gets the value '\0', so the definition of
NULL in the implementation is not relevant.

--
Salu2
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-16-2006
=?ISO-8859-15?Q?Juli=E1n?= Albo posted:

>> Yeah so what? '\0' is an integer zero.

>
> Is an integer type constant, but does not evaluate to nothing but char 0
> in this case.



Yes. The following two expressions are exactly equivalent:

(1) '\0'

(2) (char)0


"char" is a fully-fledged integer type in every way.

--

Frederick Gotham
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-16-2006
Julián Albo wrote:
> Ron Natalie wrote:
>
>>>>> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
>>>>> the value '\0' by means of habitual conversions, nothing related with
>>>>> NULL itself.
>>>> The character literal, '\0' _always_ evaluates to integer zero.
>>> Try this:
>>> cout << '\0' << '\n';
>>> cout << 0 << '\n';
>>>

>> Yeah so what? '\0' is an integer zero.

>
> Is an integer type constant, but does not evaluate to nothing but char 0 in
> this case.


char is an integer type.
>
> My first paragraph was confuse, certainly. The point is that even if NULL is
> defined as '\0' is not intended to be used like that char literal,


So, that wasn't the question. The question was "can NULL be '\0'.
I was refuting the argumnt that quoted above that "NULL is never '\0'"
and that is wrong. The language does not prohibit it.

There's nothing more or less correct about it being '\0' than 0
or 0L or false.


 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-16-2006
Frederick Gotham wrote:

>>> Yeah so what? '\0' is an integer zero.

>>
>> Is an integer type constant, but does not evaluate to nothing but char 0
>> in this case.

>
> Yes. The following two expressions are exactly equivalent:
>
> (1) '\0'
>
> (2) (char)0


Yes, both are char constants, and are used like that were appropriate.

--
Salu2
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assigning pointer to NULL Ian Collins C Programming 27 02-10-2008 10:35 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Assigning value to null Steve Caliendo ASP .Net 1 06-08-2004 04:41 PM



Advertisments