为什么将数组的地址分配给指针“my_pointer = &my_array"会出现编译错误?
问题描述:
int my_array[5] = {0};
int *my_pointer = 0;
my_pointer = &my_array; // compiler error
my_pointer = my_array; // ok
如果 my_array
是数组的地址,那么 &my_array
给我什么?
If my_array
is address of array then what does &my_array
gives me?
我收到以下编译器错误:
I get the following compiler error:
错误:无法在赋值中将 'int (*)[5]' 转换为 'int*'
error: cannot convert 'int (*)[5]' to 'int*' in assignment
答
my_array
是一个包含 5 个整数的数组的名称.编译器很乐意将其转换为指向单个整数的指针.
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
是一个指向 5 个整数数组的指针.编译器不会将整数数组视为单个整数,因此拒绝进行转换.
&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.