we can cast the three to void * , a pointer.
We can cast the 3.14 to "long", and then 3 == 3.14.
Or, we can convert (via cast or via ordinary cast-free conversion)
the 3 to "double", and then 3 != 3.14.
This shows that:
IF YOU IGNORE THE TYPE, YOU GET THE WRONG ANSWER.
Never, ever, ignore the type. C is *not* a typeless language!
(void*)&buf[0] has the same value of (void*) &buf
Probably, and perhaps even provably. (It would be hard, and perhaps
impossible, to get the rest of the C Standard's requirements right
while also doing something weird with array addressing that would,
say, encode the size of the array in the result of (void *)&buf,
so that it differs from (void *)&buf[0].) But this just means
that:
If you take value A, of type T1, and value B, of type T2,
and convert both to new values of type T3, the new values
compare equal.
This is also true for (int)3 and (double)3.14. Value A has type
int, value B has type double, and we convert both to new values of
type "char", "short", "int", or "long" -- we have lots of choices
for type T3 -- and they will compare equal.
and same value of (void*)buf.
Yes; this falls out from The Rule about pointers and arrays in C
(<
http://web.torek.net/torek/c/expr.html#therule>). The "value"
of an array object is a pointer to the array's first element.
Hence:
&buf[0] == buf
is always true -- no casts are required.
you all should notice
char * buf2 = (char*)mallic(100);
in this case we can get the address of buf2 by &buf2;
Yes. "buf2" is an object of type "pointer to char", so &buf2 is
a value of type "pointer to (pointer to char)", pointing to the
object named buf2.
Here, "buf" is an object of type "array N of T", so &buf is a
value of type "pointer to (array N of T)", pointing to the
(entire) object name buf.
buf is same as buf2 which pointer to the location where the object
lies.
No! Learning this is crucial to becoming a fully competent C
programmer: buf is *not* "the same as" buf2. The object named
"buf" is an array. The object named "buf2" is not an array.
The rules for computing their values therefore differ. The
results of &buf and &buf2 differ. The results of sizeof(buf)
and sizeof(buf2) differ.
The thing that is peculiar is that the *different* rules for
computing the "value" of buf and the value of buf2 can cause
these two different things to have the same value! That is,
after:
char buf[N]; /* for some suitable N */
char *buf2;
buf2 = buf;
the values equal, but the entities differ. The values are equal
because the "value" of an array object is that produced by The Rule
about arrays and pointers in C.
printf("buf = %p, buf2 = %p; but sizeof(buf) = %lu, sizeof(buf2) = %lu\n",
(void *)buf, (void *)buf2,
(unsigned long)sizeof buf, (unsigned long)sizeof buf2);
The different "sizeof" results proves that the two are not identical.
This is why The Rule says: "in a value context". Some operations
-- in particular, unary "&" and sizeof -- are not "value contexts".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.