Connecting Tech Pros Worldwide Forums | Help | Site Map

null pointers questions

DevarajA
Guest
 
Posts: n/a
#1: Sep 8 '05
I've read that 0 is the 'null pointer constant', and assigning it to a
pointer makes it a 'null pointer'. Is that true? And is the opposite
true? (assigning a null pointer to an int sets the int to 0)? Or should
I replace all my if(pointer) with if(pointer!=NULL)? Another question:
how does NULL evaluate?
--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org

Josh Mcfarlane
Guest
 
Posts: n/a
#2: Sep 8 '05

re: null pointers questions


DevarajA wrote:[color=blue]
> I've read that 0 is the 'null pointer constant', and assigning it to a
> pointer makes it a 'null pointer'. Is that true? And is the opposite
> true? (assigning a null pointer to an int sets the int to 0)? Or should
> I replace all my if(pointer) with if(pointer!=NULL)? Another question:
> how does NULL evaluate?[/color]

Not sure on the standard side of things, but in every compiler I've
used NULL = 0, and are interchangable. if(pointer) and if(pointer != 0)
are equivalently the same thing when dealing with pointers.

For me, setting things to NULL vs 0 is better because it shows what you
are attempting to do (bind a pointer to nothing). For evaluations, I
still use if(pointer) because I read it as if pointer exists, rather
than if pointer is not 0. Minor trivial things, but helps when people
are looking at your code.

Victor Bazarov
Guest
 
Posts: n/a
#3: Sep 8 '05

re: null pointers questions


DevarajA wrote:[color=blue]
> I've read that 0 is the 'null pointer constant', and assigning it to a
> pointer makes it a 'null pointer'. Is that true?[/color]

Yes.
[color=blue]
> And is the opposite
> true? (assigning a null pointer to an int sets the int to 0)?[/color]

No, that's not allowed without a special cast.
[color=blue]
> Or should
> I replace all my if(pointer) with if(pointer!=NULL)? Another question:
> how does NULL evaluate?[/color]

'NULL' is a macro. It usually is just 0. You can of course compare any
pointer to 0, both will be converted to _pointers_, not vice versa. You
can also use a *pointer* where a logical value is expected, then null
pointers will evaluate to 'false' and non-null will evaluate to 'true'.
So you can say

if (pointer)

which is the same as

if (pointer != 0)

V
Andre Kostur
Guest
 
Posts: n/a
#4: Sep 8 '05

re: null pointers questions


"Josh Mcfarlane" <darsant@gmail.com> wrote in news:1126214594.473166.233130
@g44g2000cwa.googlegroups.com:
[color=blue]
> For me, setting things to NULL vs 0 is better because it shows what you
> are attempting to do (bind a pointer to nothing). For evaluations, I
> still use if(pointer) because I read it as if pointer exists, rather
> than if pointer is not 0. Minor trivial things, but helps when people
> are looking at your code.[/color]

I actually prefer the other way around (for the if comparisions). I don't
like implicit tests... so I'll only use:

if (variable)

if variable is a bool. In all other cases I prefer to explicitly test it.
(comparing int's against 0 explicitly, or pointer against NULL).

if (intvar != 0)

.... or...

if (ptr != NULL)



Old Wolf
Guest
 
Posts: n/a
#5: Sep 9 '05

re: null pointers questions


Victor Bazarov wrote:[color=blue]
> DevarajA wrote:[color=green]
>> I've read that 0 is the 'null pointer constant', and assigning it to a
>> pointer makes it a 'null pointer'. Is that true?[/color]
>
> Yes.
>[color=green]
>> And is the opposite
>> true? (assigning a null pointer to an int sets the int to 0)?[/color]
>
> No, that's not allowed without a special cast.[/color]

To be more specific, an int variable with value 0 is NOT a null
pointer constant, eg. this doesn't work:

int x = 0;
void *p = x;

Only null pointer constants (and other pointers) can be assigned
to pointers. Null pointer constants must be (compile-time) constants,
such as 0, or (2 - 2), etc.

jefong@56.com
Guest
 
Posts: n/a
#6: Sep 9 '05

re: null pointers questions



Victor Bazarov wrote:[color=blue]
> DevarajA wrote:[color=green]
> > I've read that 0 is the 'null pointer constant', and assigning it to a
> > pointer makes it a 'null pointer'. Is that true?[/color]
>
> Yes.
>[color=green]
> > And is the opposite
> > true? (assigning a null pointer to an int sets the int to 0)?[/color]
>
> No, that's not allowed without a special cast.
>[color=green]
> > Or should
> > I replace all my if(pointer) with if(pointer!=NULL)? Another question:
> > how does NULL evaluate?[/color]
>
> 'NULL' is a macro. It usually is just 0. You can of course compare any
> pointer to 0, both will be converted to _pointers_, not vice versa. You
> can also use a *pointer* where a logical value is expected, then null
> pointers will evaluate to 'false' and non-null will evaluate to 'true'.
> So you can say
>
> if (pointer)
>
> which is the same as
>
> if (pointer != 0)
>
> V[/color]

Dave Rahardja
Guest
 
Posts: n/a
#7: Sep 9 '05

re: null pointers questions


On Thu, 08 Sep 2005 21:14:50 GMT, DevarajA <no@spam.com> wrote:
[color=blue]
>I've read that 0 is the 'null pointer constant', and assigning it to a
>pointer makes it a 'null pointer'. Is that true? And is the opposite
>true? (assigning a null pointer to an int sets the int to 0)? Or should
>I replace all my if(pointer) with if(pointer!=NULL)? Another question:
>how does NULL evaluate?[/color]

To add to the others' comments,

NULL is a macro that evaluates to 0 in C++. In C it evaluates to ((void*)(0)).

And...

Section 4.10.1:
A null pointer constant is an integral constant expression (5.19) rvalue of
integer type that evaluates to zero.


-dr
Gabriel
Guest
 
Posts: n/a
#8: Sep 9 '05

re: null pointers questions


DevarajA wrote:[color=blue]
> I've read that 0 is the 'null pointer constant', and assigning it to a
> pointer makes it a 'null pointer'. Is that true?[/color]

Yes, that is true. In fact, it is quite likeable that the 'obscure
internal null pointer representation' is in fact equal to 0x00000000.
But even if it isn't, the above is true.
[color=blue]
> And is the opposite
> true? (assigning a null pointer to an int sets the int to 0)?[/color]

Please do not assign a pointer to an int. It makes you live more
troubled than it might already be.
[color=blue]
> Or should
> I replace all my if(pointer) with if(pointer!=NULL)? Another question:[/color]

No, see below.
[color=blue]
> how does NULL evaluate?[/color]

NULL walks through some defines, but you can be sure that at the end
you'll find a 0. If you really want to know, you can run only the
preprocessor step with your compiler and look what the preprocessor
does. (usualle -E option)

In some cases, you might need to specify that you mean a pointer when
you write 0 (Initializing variables). Then you can write
static_cast<TypePointedTo*>(0)
to really be sure its a pointer. But do only do this if it's needed.

Gabriel
Gabriel
Guest
 
Posts: n/a
#9: Sep 9 '05

re: null pointers questions


DevarajA wrote:[color=blue]
> I've read that 0 is the 'null pointer constant', and assigning it to a
> pointer makes it a 'null pointer'. Is that true?[/color]

Yes, that is true. In fact, it is quite likeable that the 'obscure
internal null pointer representation' is in fact equal to 0x00000000.
But even if it isn't, the above is true.
[color=blue]
> And is the opposite
> true? (assigning a null pointer to an int sets the int to 0)?[/color]

Please do not assign a pointer to an int. It makes you live more
troubled than it might already be.
[color=blue]
> Or should
> I replace all my if(pointer) with if(pointer!=NULL)? Another question:[/color]

No, see below.
You can also write if (pointer != 0), but you don't need to.
[color=blue]
> how does NULL evaluate?[/color]

NULL walks through some defines, but you can be sure that at the end
you'll find a 0. If you really want to know, you can run only the
preprocessor step with your compiler and look what the preprocessor
does. (usualle -E option)

In some cases, you might need to specify that you mean a pointer when
you write 0 (Initializing variables). Then you can write
static_cast<TypePointedTo*>(0)
to really be sure its a pointer. But do only do this if it's needed.

Gabriel

Ron Natalie
Guest
 
Posts: n/a
#10: Sep 9 '05

re: null pointers questions


DevarajA wrote:[color=blue]
> I've read that 0 is the 'null pointer constant', and assigning it to a
> pointer makes it a 'null pointer'. Is that true? And is the opposite
> true? (assigning a null pointer to an int sets the int to 0)? Or should
> I replace all my if(pointer) with if(pointer!=NULL)? Another question:
> how does NULL evaluate?[/color]

You can't assign pointers to ints.

You can compare a pointer against a null pointer constant.

You can use a pointer in a condition test where a non null value
takes the meaning of true.
Ron Natalie
Guest
 
Posts: n/a
#11: Sep 9 '05

re: null pointers questions


Josh Mcfarlane wrote:
[color=blue]
> Not sure on the standard side of things, but in every compiler I've
> used NULL = 0, and are interchangable. if(pointer) and if(pointer != 0)
> are equivalently the same thing when dealing with pointers.
>[/color]
NULL is just a macro that evaluates to a null pointer constant.
0 is one of the null pointer constant values (any integral constant
expression that evaluates to zero is a null pointer constant).
Closed Thread


Similar C / C++ bytes