首页 > 解决方案 > 数组初始值设定项和指针混淆

问题描述

我了解到方括号[]实际上只是指针运算符,即

array[n] == *(array + n)

但是这在初始化一个 size 数组的上下文中是如何工作的n呢?例如

int array[n]

这和 一样int *(array + n)吗?感觉不太对。

标签: cpointers

解决方案


我了解到方括号 [] 实际上只是指针运算符

仅当它们在作为二元运算符的上下文中使用时。

/* 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));

第一行是一个声明示例,其解析方式与典型表达式不同。


推荐阅读