ANSI-C语法 - 数组声明,例如[*]等alii [英] ANSI-C grammar - array declarations like [*] et alii

查看:117
本文介绍了ANSI-C语法 - 数组声明,例如[*]等alii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 -link- 的ANSI C语法给我阵列以下规则声明:

The ANSI C grammar from -link- give me the following rules for array declarations:

 (1) | direct_declarator '[' type_qualifier_list assignment_expression ']'
 (2) | direct_declarator '[' type_qualifier_list ']'
 (3) | direct_declarator '[' assignment_expression ']'
 (4) | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']'
 (5) | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']'
 (6) | direct_declarator '[' type_qualifier_list '*' ']'
 (7) | direct_declarator '[' '*' ']'
 (8) | direct_declarator '[' ']'

现在我有一些关于这些问题:

Now I have a some questions about these:


  • 我可以用(1) - (6)除(3)仅在C99

  • (5)(4)什么是?关键字静态混淆了我。

  • 凡使用(6)?

  • 什么是以下两个函数原型之间的区别:

  • Can I use (1) - (6) except (3) only in C99?
  • What are (4) and (5) for? The keyword 'static' confuses me.
  • Where to use (6)?
  • What's the difference between the following two function prototypes:

无效美孚(INT [*]);

无效美孚(INT []);

感谢您。

推荐答案

您不能在C89 / 90数组的声明大小部分使用类型限定或静态。这些功能是专门针对C99。

You can't use type qualifiers or static in size portion of array declaration in C89/90. These features are specific to C99.

静态在数组声明告诉的编译器,你答应的指定的元素的个数总是会通过数组present作为实际参数。这可能有助于编译器生成更有效的code。如果侵犯了您的实际code承诺(即通过一个较小的数组),则该行为是不确定的。例如,

static in array declaration tells the compiler that you promise that the specified number of elements will always be present in the array passed as the actual argument. This might help compilers to generate more efficient code. If you violate your promise in the actual code (i.e. pass a smaller array), the behavior is undefined. For example,

void foo(int a[static 3]) {
  ...
}

int main() {
  int a[4], b[2];
  foo(a); /* OK */
  foo(b); /* Undefined behavior */
}

在数组声明的大小部分的 * 中的函数原型声明中使用。则表示该阵列具有可变长度(VLA)。例如,在函数定义,你可以使用VLA一个具体的运行时尺寸

The * in size portion of array declaration is used in function prototype declarations only. It indicates that the array has variable length (VLA). For example, in the function definition you can use a VLA with a concrete run-time size

void foo(int n, int a[n]) /* `a` is VLA because `n` is not a constant */
{
  ...
}

在声明原型,你可以做同样的

When you declare the prototype you can do the same

void foo(int n, int a[n]); /* `a` is VLA because `n` is not a constant */

但如果你不指定参数名称(这是在原型OK),则不能使用 N 当然的数组大小。然而,如果你还是要告诉大家,阵列将是一个VLA编译器,你可以使用 * 用于这一目的。

but if you don't specify the parameter names (which is OK in the prototype), you can't use n as array size of course. Yet, if you still have to tell the compiler that the array is going to be a VLA, you can use the * for that purpose

void foo(int, int a[*]); /* `a` is VLA because size is `*` */

请注意,与一维数组的例子不是一个好。即使你省略 * ,并宣布上述功能

Note, that the example with a 1D array is not a good one. Even if you omit the * and declare the above function as

void foo(int, int a[]);

然后code仍将正常工作,因为在函数参数声明数组类型是隐含指针类型取代反正。但是,一旦你开始使用多维数组,正确使用的* 变得很重要。例如,如果该函数被定义为

then the code will still work fine, because in function parameter declarations array type is implicitly replaced with pointer type anyway. But once you start using multi-dimensional arrays, the proper use of * becomes important. For example, if the function is defined as

void bar(int n, int m[n][n]) { /* 2D VLA */
  ...
}

的原型可能如下所示

the the prototype might look as follows

void bar(int n, int m[n][n]); /* 2D VLA */

void bar(int, int m[*][*]); /* 2d VLA */

在后一种情况下的第一个 * 可以省略(因为数组到指针替换),而不是第二个 *

In the latter case the first * can be omitted (because of the array-to-pointer replacement), but not the second *.

这篇关于ANSI-C语法 - 数组声明,例如[*]等alii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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