首页 > 解决方案 > 将整数数组分配给整数指针时出错

问题描述

我需要了解编译错误。

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地址做了什么。

标签: c++arrayspointerscompiler-errors

解决方案


yocto是一个数组,但是当作为参数分配给一个指针时,它会衰减为一个指向数组第一个元素的指针。 &yocto是指向数组的指针。具体来说,指向一个指向元素int数组的指针4,因为它是什么yocto,所以&yocto它的类型int (*)[4]


推荐阅读