gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:20:00 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com