【发布时间】:2021-03-12 18:25:51
【问题描述】:
我需要了解编译错误。
int main(int argc, char *argv[])
{
int yocto[] = {100, 200, 300, 400};
int *android = &yocto; // Getting error cannot convert ‘int (*)[4]’ to ‘int*’ in initialization
int *linux = yocto; // Working fine.
// But when I am printing the value of &yocto and yocto giving same address.
cout<<"Value of &yocto : " <<&yocto<<endl; // 0x7ffc5f553e50
cout<<"Value of yocto : "<<yocto<<endl; // 0x7ffc5f553e50
return 0;
}
请解释一下编译器在内部使用&yocto 地址做什么。
【问题讨论】:
-
想象一下像激光笔一样的指针。您可以指向
ints、数组、结构等...是绿色的,.., ..., .... 现在计算数组{ 1, 2, 3, 4 }和一个指向数组的绿色指针,同时还有一个指向数组第一个元素的红色指针。指针指向同一个地方吗? :-) -
改写为,“为什么
int[4]可以衰减为int*,而int(*)[4]不能?”可能有重复。应该澄清的是,这两行都试图从一种类型转换为另一种类型。 -
您的
int *android = &yocto;应该是int *android = &yocto[0];否则您将数组的地址分配给整数的地址。数组不是整数。
标签: c++ arrays pointers compiler-errors