Originally Posted by
George2
I am very interested in how C treats foo and &foo and make them the same?
It has been pointed out that these two items will have same value, however they are not the same.
For an array of "anything":
The name of the array used by itself (no [] brackets) is taken to be a const pointer to "anything", whose value is the address of the first element of the array.
Therefore if "foo" is an array of char, "foo" and "&foo[0]" are treated the same way: foo is a pointer to char, &foo[0] is a pointer to char. The notation for foo and &foo[0] is interchangeable.
However, if foo is an array of, say, 100 chars, then &foo is a pointer to an array of 100 chars. That is not the same as a pointer to char.
Consider the following:
Code:
#include <stdio.h>
int main()
{
char foo[100];
char *p1;
char *p2;
char *p3;
char (*p4)[100]; /* a pointer to an array of 100 chars */
p1 = foo;
p2 = &foo[0];
p3 = &foo;
p4 = &foo;
printf("p1 = %p, p1+1 = %p\n", (void *)p1, (void *)(p1+1));
printf("p2 = %p, p2+1 = %p\n", (void *)p2, (void *)(p2+1));
printf("p3 = %p, p3+1 = %p\n", (void *)p3, (void *)(p3+1));
printf("p4 = %p, p4+1 = %p\n", (void *)p4, (void *)(p4+1));
return 0;
}
Now, with gcc, the assignment statement for p3 is flagged with a "warning: assignment from incompatible pointer type". Since it's only a warning, an executable is generated.
The output is:
Code:
p1 = 0xbfa160c0, p1+1 = 0xbfa160c1
p2 = 0xbfa160c0, p2+1 = 0xbfa160c1
p3 = 0xbfa160c0, p3+1 = 0xbfa160c1
p4 = 0xbfa160c0, p4+1 = 0xbfa16124
So, used this way, we can see that, indeed, the value of the value of the address of the array is equal to the value of the address of the first element of the array. (What else would it be?)
However---look at the result of pointer arithmetic for p4. The value of p4+1 is 100 (decimal) larger than the value of p4.
One further note:
If you try to compile this program with g++, the compiler flags the assignment statement for p3 with " error: cannot convert ‘char (*)[100]’ to ‘char*’ in assignment." and no executable is created. In other words, C++ is a little more picky about what it lets you do with pointers. (Requires an explicit reinterpret_cast. That's a good thing, right?) Removing the assignment and print statements for p3 results in a clean compile with gcc as well as with g++, and, of course, the results are the same.
Bottom line: Sloppy or unknowing C programmers who use "&foo" instead of "foo" or "&foo[0]" may actually get something that does what they want it to do (for example, strcpy() works the same), but why would you do such a thing, knowing what this simple example tells us? Namely that they are not the same, and, depending on how you use them, you could get different results.
D