c - 数组初始值设定项和指针混淆
问题描述
我了解到方括号[]
实际上只是指针运算符,即
array[n] == *(array + n)
但是这在初始化一个 size 数组的上下文中是如何工作的n
呢?例如
int array[n]
这和 一样int *(array + n)
吗?感觉不太对。
解决方案
我了解到方括号 [] 实际上只是指针运算符
仅当它们在作为二元运算符的上下文中使用时。
/* in this case, [] is being used to specify the size of the array, it is not
being used as an indexing operator */
int a[n];
/* in this case, [] is being used as an indexing operator, so the following two are equivalent */
do_something(a[i]);
do_something(*(a+i));
第一行是一个声明示例,其解析方式与典型表达式不同。
推荐阅读
- haskell - 从列表中选择特定图片
- octave - 如何在 Octave 中无损地将双精度转换为字符串并返回
- javascript - 无法将数据保存在 chrome 扩展存储中
- html - CSS 动画在 IE11、Windows 7 和 Windows 10 中不起作用
- javascript - jquery .change(function) 不能调用类函数
- python - 如何更改熊猫数据框单元格中名称的确切出现
- python - Python 3 如何找到命名为字符串的变量?
- python - Pandas 数据框去除非数字字符
- node.js - ReactJS:如何在本地服务器上部署
- javascript - 贝塞尔曲线数学