gpt4 book ai didi

c++ - C++中数组索引的类型

转载 作者:IT老高 更新时间:2023-10-28 22:25:55 35 4
gpt4 key购买 nike

C++ 编程语言中数组索引的类型是什么?例如在这样的声明中:

int tab[5];

5 转换成什么类型​​?或者它只是普通的 int?

最佳答案

在该代码中,5 只是一个普通的整数文字,所以这里只是一个普通的 int

§8.3.4 Arrays in n3290 (~ C++11) 指定数组声明符:

In a declaration T D where D has the form

D1 [ constant-expressionopt ] attribute-specifier-seqopt

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is an array type; if the type of the identifier of D contains the auto type-specifier, the program is ill-formed. T is called the array element type; this type shall not be a reference type, the (possibly cv-qualified) type void, a function type or an abstract class type. If the constant-expression (5.19) is present,it shall be an integral constant expression and its value shall be greater than zero.

§5.2.1 下标描述了表达式中括号内的内容:

A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type “pointer to T” and the other shall have unscoped enumeration or integral type.The result is an lvalue of type “T.” The type “T” shall be a completely-defined object type. The expressionE1[E2] is identical (by definition) to *((E1)+(E2))

关于c++ - C++中数组索引的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8111357/

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