Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

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

Apparently the following function prototypes are valid in C99 and 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?

Do they help distinguish statically typed arrays from variable-length arrays? Or are they just syntactic sugar?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

static in parameter array declarator

 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.

A compiler has then the right to assume f argument is not NULL and therefore it could perform some optimizations. gcc currently performs no optimization (source):

"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."

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). For example, with const it means a is a const pointer to int (i.e., type 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...