简体   繁体   English

数组初始化期间的类型

[英]Type during array Initialization

If in my program, I have this: 如果在我的程序中,我有这个:

int arr[some_number];

What is the type of some_number ? some_number的类型是some_number

  1. Integer? 整数?
  2. Unsigned integer? 无符号整数?
  3. Automatically determined ( long , unsigned long etc.) 自动确定( longunsigned long等)

This might be a hypothetical question (assuming I can allocate as much memory as needed at compile time), just curious to know if type of some_number is always integer. 这可能是一个假想的问题(假设我可以在编译时分配所需的内存),只是想知道some_number类型是否总是整数。

* *EDIT In case my language is not clear, on a system where sizeof(integer) is 2 bytes and I define array like: * * EDIT如果我的语言不清楚,在sizeof(integer)是2个字节的系统上,我定义如下数组:

int arr[65537] , will "65537" overflow and it is effectively, int arr[-1]? int arr [65537],“ 65537”是否会溢出,实际上是int arr [-1]?

some_number must either be an actual positive integer as in:- some_number必须是实际的正整数,例如:

int arr[1024]

or it can be a MACRO which resolves to a positive integer:- 也可以是可以解析为正整数的MACRO:

#DEFINE some_number 1024
int arr[some_number]

As the interpretation is done at compile time and there are no program variable is used then there is no "type". 由于解释是在编译时完成的,没有使用程序变量,因此没有“类型”。

By default in C, the type of a number is int . 在C中,默认情况下,数字的类型为int You can use the suffix u to make it an unsigned integer, the suffix l to make it a long (and with some compiler the suffix ll to make it a long long , ie. a 64-bit integer). 您可以使用后缀u使其成为一个无符号整数,使用后缀l使其成为一个long整数(对于某些编译器,使用后缀ll使它成为一个long long ,即64位整数)。

Type of an expression doesn't depend on the context, so the type of some_number ( some_expression actually) will be the same as if it was used not in the array definition. 表达式的类型不依赖于上下文,所以类型some_numbersome_expression实际上)将是相同的,就好像它是在数组定义不被使用。 Another question is what types of expressions are allowed for designation of the array size. 另一个问题是可以使用哪种类型的表达式来指定数组大小。

I think it is of type size_t , which can differ from one platform to another. 我认为它的大小为size_t ,这可能在一个平台与另一个平台之间有所不同。 It's unsigned, that's for sure. 它是未签名的,这是肯定的。 Look at this . 这个

some_number must be TRUE constant ,it cann't be variable. some_number必须为TRUE常量,不能为变量。 for example 例如

 main()
{
int a=1;
int kk[a]={1};
}

It would result an error saying 这将导致错误提示

 variable-sized object may not be initialized

However we can not also use 但是我们不能同时使用

int const a;

as array subscript beacuse const is not consider as true constant in C. 因为数组下标是因为const在C中不被视为真正的常数。

Supposing this is a definition inside a function, some_number can be any integral expression with a strictly positive value. 假设这是一个函数内部的定义, some_number可以是任何带有严格正值的整数表达式。 If it is non-constant, the beast is called variable length array , VLA. 如果它不是恒定的,则该野兽称为可变长度数组 VLA。

If you place it in outer scope or make it static , it has to evaluate to something that is constant. 如果将其放置在外部范围中或使其变为static ,则它必须求值为常数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM