函数参数声明中的静态数组下标 (仅限 C)

除了在某些上下文中,未下标的数组名 (例如, region 而不是 region[4]) 表示一个指针,该指针的值是数组的第一个元素的地址,前提是先前已声明该数组。 函数的参数列表中的数组类型也会转换为相应的指针类型。 从函数主体中访问数组时,将丢失有关自变量数组大小的信息。

为了保留此信息 (对于优化很有用) , C99 允许您使用 static 关键字来声明自变量数组的索引。 常量表达式指定可用作优化假设的最小指针大小。 static 关键字的这种特殊用法是高度规定的。 该关键字只能出现在最外层的数组类型派生中,并且只能出现在函数参数声明中。 如果函数的调用者不遵守这些限制,那么行为未定义。

以下示例显示了如何使用该功能部件。
void foo(int arr [static 10]);       /* arr points to the first of at least
                                           10 ints                           */
void foo(int arr [const 10]);        /* arr is a const pointer               */
void foo(int arr [static const i]);  /* arr points to at least i ints;
                                           i is computed at runtime.         */
void foo(int arr [const static i]);  /* alternate syntax to previous example */
void foo(int arr [const]);           /* const pointer to int                 */