473,324 Members | 2,257 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,324 software developers and data experts.

null pointers questions

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
Sep 8 '05 #1
10 1526
DevarajA wrote:
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?


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.

Sep 8 '05 #2
DevarajA wrote:
I've read that 0 is the 'null pointer constant', and assigning it to a
pointer makes it a 'null pointer'. Is that true?
Yes.
And is the opposite
true? (assigning a null pointer to an int sets the int to 0)?
No, that's not allowed without a special cast.
Or should
I replace all my if(pointer) with if(pointer!=NULL)? Another question:
how does NULL evaluate?


'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
Sep 8 '05 #3
"Josh Mcfarlane" <da*****@gmail.com> wrote in news:1126214594.473166.233130
@g44g2000cwa.googlegroups.com:
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.


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)

Sep 8 '05 #4
Victor Bazarov wrote:
DevarajA wrote:
I've read that 0 is the 'null pointer constant', and assigning it to a
pointer makes it a 'null pointer'. Is that true?


Yes.
And is the opposite
true? (assigning a null pointer to an int sets the int to 0)?


No, that's not allowed without a special cast.


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.

Sep 8 '05 #5

Victor Bazarov wrote:
DevarajA wrote:
I've read that 0 is the 'null pointer constant', and assigning it to a
pointer makes it a 'null pointer'. Is that true?


Yes.
> And is the opposite
true? (assigning a null pointer to an int sets the int to 0)?


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


'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


Sep 9 '05 #6
On Thu, 08 Sep 2005 21:14:50 GMT, DevarajA <no@spam.com> wrote:
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?


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
Sep 9 '05 #7
DevarajA wrote:
I've read that 0 is the 'null pointer constant', and assigning it to a
pointer makes it a 'null pointer'. Is that true?
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.
And is the opposite
true? (assigning a null pointer to an int sets the int to 0)?
Please do not assign a pointer to an int. It makes you live more
troubled than it might already be.
Or should
I replace all my if(pointer) with if(pointer!=NULL)? Another question:
No, see below.
how does NULL evaluate?


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
Sep 9 '05 #8
DevarajA wrote:
I've read that 0 is the 'null pointer constant', and assigning it to a
pointer makes it a 'null pointer'. Is that true?
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.
And is the opposite
true? (assigning a null pointer to an int sets the int to 0)?
Please do not assign a pointer to an int. It makes you live more
troubled than it might already be.
Or should
I replace all my if(pointer) with if(pointer!=NULL)? Another question:
No, see below.
You can also write if (pointer != 0), but you don't need to.
how does NULL evaluate?


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

Sep 9 '05 #9
DevarajA wrote:
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?


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.
Sep 9 '05 #10
Josh Mcfarlane wrote:
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.

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).
Sep 9 '05 #11

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

Similar topics

1
by: Denzil | last post by:
Hi, I am retrieving my DB values and setting them in the MyRS::DoFieldExchange(CFieldExchange* pFX) function. One of these records is a Long datatype and could be "Null" in the DB table. The...
16
by: mike79 | last post by:
Hi all, I have a the following simple piece of code which has taken me hours to try and sort out the problem, but still unable to find what is wrong. void main( void ) { char (*string);...
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
42
by: polas | last post by:
Afternoon all, I have some code (which I thought was OK, but now appreciate is probably not by the standard) which contains int a; ...... if (a==NULL) { ....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.