Why is it a compile error to assign the address of an array to a pointer “my_pointer = &my_array”?

后端 未结 3 1312
北荒
北荒 2020-12-09 18:10
int my_array[5] = {0};
int *my_pointer = 0;

my_pointer = &my_array // compiler error
my_pointer = my_array // ok

If my_array is a

3条回答
  •  旧时难觅i
    2020-12-09 18:54

    my_array is the name of an array of 5 integers. The compiler will happily convert it to a pointer to a single integer.

    &my_array is a pointer to an array of 5 integers. The compiler will not treat an array of integers as a single integer, thus it refuses to make the conversion.

提交回复
热议问题