Is there an implicit &
calculation when cast array to pointer?
Of other type cast, there is not such &
calculation.
First we know array is not pointer, though array and pointer have lots of same or similar operations.
When I try this code, I found that they all have same output:
typedef struct _test_struct {
char a[10];
} test_t;
int main() {
test_t *instance = malloc(sizeof(test_t));
printf("%zu\n", sizeof(test_t));
printf("%p\n", instance);
printf("%p\n", (instance->a));
printf("%p\n", &(instance->a));
return 0;
}
// output is:
10
0x7fdb53402790
0x7fdb53402790
0x7fdb53402790
The last two lines shows that value of array casted to pointer is the same as value of address of array.
In my understanding of casting, it's understanding a same value in a different way(bits expanding or truncating may be applied). For example, cast int a = 0;
into a pointer (void *)a
is actually just getting a pointer pointing at address 0.
But in the code above, there is no value 0x7fdb53402790
associated with both instance
or a
, so when do casting we can't just re-understand this value as a pointer(since there isn't such a value to re-understand at all).
So I guess when casting an array to a pointer, there is an implicit &
address of calculation. Is my understanding right?