c++ - 将整数数组分配给整数指针时出错
问题描述
我需要了解编译错误。
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]
推荐阅读
- vue.js - `vue-cli-service build` 不生成模板的代码
- date - Vue没有显示日期值
- r - R 相关性:具有缺失数据的配对向量的条件处理
- javascript - 具有许多 X 值的缩放图表
- java - 当我使用递归函数时,它返回意外的结果
- wagtail - 如何使用“permissions_required”挂钩有效地将非 CMS 页面视图限制为 wagtail 中的用户组?
- maven - Maven - 通过 groupId 更新依赖项的命令行
- angular - 在路由器出口注入的延迟加载模块中使用面包屑组件
- angular - 当我绑定到一个数组时,它似乎没有正确反映到标记 ngif
- ruby - Ruby:通过解析规范化 URL