Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Meet and Greet (http://www.go4expert.com/community/meet-and-greet/)
-   -   Are these two statements same? (http://www.go4expert.com/community/statements-t19773/)

amitnvaidya 16Oct2009 01:50

Are these two statements same?
 
int *ptr = "NULL"; // Here is a statement

int *ptr; // Here is a set of two statements
*ptr = "NULL";

/*
when
you print the first
it gives 0
and second if you print
it gives linking error
why so
*/

shabbir 16Oct2009 08:56

Re: Are these two statements same?
 
NULL should be NULL and not "NULL" as "NULL" one is a string literal

amitnvaidya 16Oct2009 12:44

Re: Are these two statements same?
 
Thats agreed... whats the result in the following case.

int *ptr= NULL;
printf("%d",ptr);



int *ptr;
*ptr = NULL;
printf("%d",ptr);

SaswatPadhi 16Oct2009 20:17

Re: Are these two statements same?
 
In the first case, you assign NULL to the pointer (the address), NOT to the value pointed by it.
In the second case, you assign NULL to the value pointed by the pointer.
So, the cases are not equivalent.

These are equivalent :

int *ptr = NULL;
printf("%d", ptr);

int *ptr;
ptr = NULL;
printf("%d",ptr);

And should have equal behaviour on compilation. (Try them !)


All times are GMT +5.5. The time now is 03:01.