Joe Wright wrote On 08/09/07 18:01,:
Eric Sosman wrote:
somenath wrote On 08/09/07 10:12,:
Hi All.
I would like to know the following information.
1)Is there any difference between the address and integer ?
For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;
My doubt is there any difference between the properties of value of x
and address of y which is 500.
Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address. Contemplate this code fragment
for a while, and you'll understand why a pointer is more
than an address:
union { double d; int i; } u;
double *dp = &u.d;
int *ip = &u.i;
In this code, the two pointers dp and ip point to the
same memory location. Nonetheless, they are different.
What makes them different?
They have different type. They have identical value. They have
identical size (4). They cannot be distinguished from one another
except by the compiler. Why do you think they are different?
Because `array[*ip]' might work (given a suitable
value in `u.i') while `array[*dp]' won't even compile.
Because `ip = dp' won't even compile.
Because `sizeof *ip != sizeof *dp' (probably).
Because `*dp = DBL_MAX' is legal but `*ip = DBL_MAX'
probably invokes undefined behavior.
Because `if ((*dp = DBL_MIN))' branches oppositely
to `if ((*ip = DBL_MIN))'.
Because `printf ("%f\n", *dp)' works (with a suitable
value in `u.d') but `printf ("%f\n", *ip)' invokes undefined
behavior, always.
Because we've moved beyond B.