简体   繁体   English

C99中那些奇怪的数组大小[*]和[静态]是什么?

[英]What are those strange array sizes [*] and [static] in C99?

Apparently the following function prototypes are valid in C99 and C11: 显然,以下函数原型在C99和C11中有效:

void foo(int a[const *]);

void bar(int a[static volatile 10]);

What is the purpose of those strange subscript notations * , static , and CV qualifiers? 那些奇怪的下标符号*static和CV限定符的目的是什么?

Do they help distinguish statically typed arrays from variable-length arrays? 它们有助于区分静态类型数组和可变长度数组吗? Or are they just syntactic sugar? 或者他们只是语法糖?

static in parameter array declarator 参数数组声明符中的static

 void f(int a[static 10]);

static here is an indication that parameter a is a pointer to int but that the array objet (where a is a pointer to its first element) has at least 10 elements. static here表示参数a是指向int的指针,但是数组objet(其中a是指向其第一个元素的指针)至少有10元素。

A compiler has then the right to assume f argument is not NULL and therefore it could perform some optimizations. 然后编译器有权假设f参数不是NULL ,因此它可以执行一些优化。 gcc currently performs no optimization ( source ): gcc目前不执行任何优化( 来源 ):

"The information provided by static in parameter array declarators is not used for optimization. It might make sense to use it in future in conjunction with work on prefetching." “参数数组声明符中的static提供的信息不用于优化。将来与预取工作结合使用可能是有意义的。”

qualifier in parameter array declarator 参数数组声明符中的限定符

void g(int a[cvr 10]);

inside g a is a cvr pointer to int ( cvr is const , volatile or restrict qualifier). inside g a是一个指向intcvr指针( cvrconstvolatilerestrict限定符)。 For example, with const it means a is a const pointer to int (ie, type int * const ). 例如,对于const它表示a是一个指向intconst指针(即,类型为int * const )。

So a parameter declaration: 所以参数声明:

T param[cvr e] 

is the same as a parameter declaration: 与参数声明相同:

T * cvr param

* in parameter array declarator * 在参数数组声明符中

void h(int a[*]);

The [*] in a formal array parameter declaration in a function declaration (that is not part of a function definition) indicates that the formal array is a variable length array. 函数声明中的正式数组参数声明中的[*] (不是函数定义的一部分)表示正式数组是可变长度数组。

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

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