zero vs. null pointer in c++

Status
Not open for further replies.

mif

Registered Member
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~
 
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...


Originally posted by mif
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~
 
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.
 
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!
 
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

Originally posted by zion
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!
 
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.
 
0 for primitive types
null for Object types

for all I know :confused:

I use java, which is an offshoot of C, also OOP
 
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
<><

Originally posted by zion
Hey Anton,what happened to that Shortest path stuff??!!

just curious...




bye!
 
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!
 
No problem :)

My biggest concern right now isnt shortest path algorith...those are basically pretty simple. Im looking at devising some pattern finding algorithms.

-AntonK
<><
 
Status
Not open for further replies.
Back
Top