为什么将数组的地址分配给指针“my_pointer = &my_array"会出现编译错误? [英] Why is it a compile error to assign the address of an array to a pointer "my_pointer = &my_array"?
本文介绍了为什么将数组的地址分配给指针“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.
这篇关于为什么将数组的地址分配给指针“my_pointer = &my_array"会出现编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文