C99 中那些奇怪的数组大小 [*] 和 [静态] 是什么? [英] What are those strange array sizes [*] and [static] in C99?

查看:19
本文介绍了C99 中那些奇怪的数组大小 [*] 和 [静态] 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然以下函数原型在 C99 和 C11 中有效:

Apparently the following function prototypes are valid in C99 and C11:

void foo(int a[const *]);

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

那些奇怪的下标符号*static和CV限定符的目的是什么?

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?

推荐答案

static in parameter array declarator

 void f(int a[static 10]);

static 这里表明参数 a 是一个指向 int 的指针,但数组对象(其中 acode> 是指向其第一个元素的指针)至少有 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.

然后编译器有权假设 f 参数不是 NULL,因此它可以执行一些优化.gcc 当前不执行任何优化(source):

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

参数数组声明符中的限定符

void g(int a[cvr 10]);

inside g a 是一个 cvr 指向 int 的指针(cvrconstvolatilerestrict 限定符).例如,const 意味着 a 是一个 const 指向 int 的指针(即类型 int* const).

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

所以是参数声明:

T param[cvr e] 

与参数声明相同:

T * cvr param

* 在参数数组声明符中

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.

这篇关于C99 中那些奇怪的数组大小 [*] 和 [静态] 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆