# zero vs. null pointer in c++

Discussion in 'Computer Science & Culture' started by mif, Nov 4, 2002.

Not open for further replies.
1. ### mifRegistered Member

Messages:
2
Hey all c++ programmers!

can anyone tell me what is the difference between using

int *r = 0 vs. int *r = NULL?

why do you use one over the other?

Thanks~

3. ### AgamemnoNRegistered Senior Member

Messages:
58
As far as my knowledge about c/c++ goes, NULL it's just a named constant for the value zero... So Why do i use NULL instead of just using 0? it's quite simple, code organization, every time i have to start a pointer, i set it to NULL, because just of seeing the name, i know that the object/variable will be a pointer,

Look at these two lines

int a = 0;
int a = NULL;

or

int *a=0;
int *a=NULL;

the result int this lines will be always the same, but for a programmer that will read the code, it's much more intuitive to read a pointer as NULL and an integer as zero..

And also, NULL it's the oficial address for nothing, so if the ANSI want to change this value from 0 to 2E-10 and you always use the named constant, than you'll have no problem with that, but if you use zero, than you'll have to change all the code you made before...

The second thought is just a little terrorism, i use NULL because of the organization and easy understanding... and with my experience i can assure you, sometimes a little organization can save a lot of time when you're debuging and looking for errors in your code...

5. ### fadingCaptainare you a robot?Valued Senior Member

Messages:
1,762
I haven't programmed in C++ for awhile but from what I remember, aga is right. NULL and '0' are the same.

It is more intuitive for me to use NULL with pointers...

Purely, a stylistic thing.

7. ### RickॐValued Senior Member

Messages:
3,336
Okay let me take the meaning of what you have asked:

first
Code:

int *r =0

lets say that r contains address of a.
so '*' operator means value at address <i>contained</i> in r.so r contains address of a.so this statement means that the value of a is 0.

got it?

if you dont get it,read theabove statement couple of times,you"ll get it surely...

bye!

8. ### AntonRegistered Member

Messages:
8
Nope....

Nope, sorry. The * operator has a different meaning depending on how you use it. In this case it is not a derefrencing operator. It is actually use with the int and not the r.

int *r is the same as int* r,
where int* denotes that whatever the next word is, will be a variable that points to an integer. because there is an equals sign after it, that means that the variable will point to something initially, it wil point to memory location 0, also known as NULL.
Now...if he had somewhere else in his code used the *, then it is a derefencing operator. Example:

int *r=0;
int a = 5;
r = &a;
*r = 6;

NOW -- if u print out the value of a, it is 6. If you check the value of *r, it is also 6. In this case it IS a derefrencing operator.

-Anton Kiriwas

9. ### sjmarshaRegistered Senior Member

Messages:
363
In C NULL and 0 are the same.

Although they can have different meanings depending on when they are used.

i.e. I could assign a flag the value of zero, but would that be like saying I gave it a NULL value? Of course not.

However, In the examples given above they mean the same.

10. ### oscarconfusoidRegistered Senior Member

Messages:
315
0 for primitive types
null for Object types

for all I know

I use java, which is an offshoot of C, also OOP

11. ### RickॐValued Senior Member

Messages:
3,336
Hey Anton,what happened to that Shortest path stuff??!!

just curious...

bye!

12. ### AntonKTechnomageRegistered Senior Member

Messages:
1,083
Shortest path??? Hmmmm *Anton performs a binary search on his brain before remembering theres NO WAY his brain has been sorted first

*
I do a lot of programming..vaguely remember posting somethng about that...refresh me? Sorry...too many languages, keywords and semaphors to remember normal human stuff

-AntonK
<><

13. ### RickॐValued Senior Member

Messages:
3,336
OOPs sorry for increase in Time Complexity

...i thought it was you who asked in a thread about how to make a shortest path algo...but it was a guy called Anis i think...

terribly sorry...

bye!

Messages:
1,083
No problem