微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将整数数组分配给整数指针时出错

如何解决将整数数组分配给整数指针时出错

我需要了解编译错误

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

解决方法

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

的类型>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。