-1

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?

Buqian Zheng
  • 301
  • 4
  • 10
  • 2
    [don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714), and [print the result of `sizeof` with `%zu`](https://stackoverflow.com/q/940087/995714) – phuclv Jul 16 '17 at 16:26
  • @LưuVĩnhPhúc Thank you – Buqian Zheng Jul 16 '17 at 16:30
  • @Jean-FrançoisFabre Thank you, I know `&instance` will give me a different value(one address saving another address), but I think it's irrelevant to my question. – Buqian Zheng Jul 16 '17 at 16:32
  • @LưuVĩnhPhúc the "%p" arguments should also be explicitly cast to void*, or else ... the behavior is undefined. – Petr Skocik Jul 16 '17 at 16:39

1 Answers1

1

Firstly, there's no need to do the casting specifically in order to conver array to pointer. The term cast normally refers to explicit conversions. The array-to-pointer conversion is usually performed implicitly.

Secondly, array-to-pointer conversion is not "value of array casted to pointer". Array-to-pointer conversion works in accordance with its own special rules. You can indeed think of array-to-pointer conversion as a hidden application of & operator. However, for array a the proper application is not &a, but raher &a[0]. So, when you do

char a[10];
char *p = a;

the last line can be seen as equivalent to

char *p = &a[0];

(This is a rather flawed "circular" explanation, since operator [] itself works through array-to-pointer conversion, but the point is that that hidden & operator is applied to the first element of the array, not to the whole array.)

This means that the &(instance->a) in your example is not really a good representation of how array-to-pointer conversion works, even if it happens to produce a pointer with the same numerical value.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Thank you for your response. I have mostly understand. But why is `&a` not a proper application? This is just treating the whole array as one element and get its address, so `&a+1` jump across the whole array(though this is rarely useful). – Buqian Zheng Jul 16 '17 at 17:08
  • 1
    @Buqian Zheng: The result of array-to-pointer conversion is a pointer to *an element*. For `char a[10]` array the result of array-to-pointer conversion should have `char *` type. The `&a` would provide a pointer to *the entire array*, i.e. a pointer of type `char (*)[10]`. That's why the `&a` is not a good analogy. The numerical value of the `&a` pointer is correct, but *the type is wrong*. – AnT stands with Russia Jul 16 '17 at 18:12