c - 运算符 [] 的正确输入数据类型是什么?

标签 c arrays memory

当访问一个数组时,我们使用运算符[],如下所示:

int a[5];
...
a[b] = 12;

上面的变量 b 的正确数据类型是什么?

我发现 a[b] 等同于 *(a + b),这让我觉得我想要 bvoid*size_t 但我不确定。

最佳答案

来自 C 标准(ISO/IEC 9899:TC2) Sec 6.5.2.1 数组下标

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

关于c - 运算符 [] 的正确输入数据类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17810995/

相关文章:

ruby-on-rails - 有没有更有效的方法来存储商店的营业时间?

c++ - 从 RGB BMP 创建灰度 BMP

c - "assignment makes integer from pointer without a cast -wint-conversion"

c - 删除二叉堆中的最顶层元素

python - 如何在 Python 中创建矩阵或将二维数组转换为矩阵?

java - 使用数组编号计算后给出错误答案

c++ - CL_MEM_ALLOC_HOST_PTR 比 CL_MEM_USE_HOST_PTR 慢

c - 为什么我不能在 BSS 中获取静态变量?

c - 字符串的实现

c - 1/(1.0/0.0) 在 C 中计算为零