Consider the following test program (codepad execution):
#include <stdio.h>
#include <string.h>
struct camp {
char b[8];
};
int main()
{
struct camp c;
strcpy(c.b, "Hello");
c.b[5] = '\0';
printf("c size: %d\nAddress (amp): %d :: %d\n", sizeof(c), &c, c);
printf("Address of b: %d :: %d\n", &(c.b), c.b);
return 0;
}
A sample output:
c size: 8
Address (amp): -1082463628 :: 1819043144
Address of b: -1082463628 :: -1082463628
Whereas the address given by &(c.b) and c.b (second call to printf) is same, the same for struct camp c (first call to printf) returns different address. Furthermore, &c being same as &(c.b) or c.b.
What exactly is happening?
What
printfis trying to do in the first case is to interpretcas an integer.cis not an integer, or even a value that can be converted to an integer (explicitly or implicitly), so the value written can be anything as you are invoking undefined behavior.