为什么使用星号“[*]"?而不是函数的 VLA 数组参数的整数? [英] Why use an asterisk "[*]" instead of an integer for a VLA array parameter of a function?

查看:20
本文介绍了为什么使用星号“[*]"?而不是函数的 VLA 数组参数的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数中使用可变长度数组作为参数时

When using variable-Length Array as parameter in function

int sum(int n, int a[n]);

很容易理解第一个参数(n)指定了第二个参数(a)的长度.但是遇到了另一个用于 VLA 作为参数的原型

it is easy to understand first parameter(n) specifies the length of the second parameter(a). But encountered with another prototype used for VLAs as parameter

int sum(int n, int a[*]);

真的很难理解为什么在[]里面使用*而不是n?

is really difficult to understand why * is used instead of n inside []?

推荐答案

[*] 语法旨在用于声明函数原型.这里的关键细节是,在函数原型中,您不需要为参数命名,您只需指定每个参数的类型.

The [*] syntax is intended to be used when declaring function prototypes. The key detail here is that in function prototypes you are not required to name your parameters, you just have to specify the type of each parameter.

在您的示例中,如果您保留第一个参数 未命名,那么显然您将无法在第二个(数组)参数声明中使用 n.然而,在许多情况下,您必须告诉编译器某些参数是 VLA.这是 [*] 语法派上用场的时候.

In your example, if you leave the first parameter unnamed, then obviously you won't be able to use n in your second (array) parameter declaration. Yet, in many cases you have to tell the compiler that some parameter is a VLA. This is when the [*] syntax comes to the rescue.

在你的情况下,如果你省略参数名称,原型可能看起来像

In your case, if you omit the parameter names, the prototype might look as

int sum(int, int [*]);

但是,重要的是要注意,在您的特定示例中,此语法是合法的,但并非完全必要.就像非 VLA 数组一样,int [n] 参数仍然等效于 int * 参数(即使对于非常量 n).这意味着您可以简单地将您的函数原型为

However, it is important to note that in your specific example this syntax is legal, but it is not exactly necessary. Just like with non-VLA arrays, an int [n] parameter is still equivalent to int * parameter (even for non-constant n). This means that you can simply prototype your function as

int sum(int, int []);

或作为

int sum(int, int *);

并且原型仍将用于其目的,即它将正确匹配函数定义.换句话说,声明为一维数组的参数的 VLA 属性完全无关紧要,并且此类 VLA 数组并不真正需要 [*] 特性.

and the prototype will still serve its purpose, i.e. it will properly match the function definition. In other words, VLA properties of a parameter declared as an 1D array are completely inconsequential and the [*] feature is not really needed with such VLA arrays.

[*] 在类型的可变数组性"没有丢失的情况下变得很重要,就像 2D VLA(或指向 VLA 的指针)一样.例如.定义为

The [*] becomes important in situations when the "variable arrayness" of the type is not lost, as would be the case with 2D VLA (or a pointer to a VLA). E.g. a function defined as

int sum2d(int n, int m, int a[n][m])
{
  ...
}

可能被原型化为以下任何一种

might be prototyped as any of the following

int sum2d(int, int, int a[*][*]);
int sum2d(int n, int, int a[n][*]);
int sum2d(int, int m, int a[*][m]);
int sum2d(int n, int m, int a[n][m]);

以上所有原型都与函数定义正确匹配.

All of the above prototypes properly match the function definition.

当然,如果你有在函数原型中总是命名所有参数的习惯,那么你就永远不需要这个 [*] 语法,因为你将能够在函数原型中使用最后一个原型上面的列表.

Of course, if you have the habit of always naming all parameters in function prototypes, then you'll never need this [*] syntax, since you will be able to use the last prototype in the above list.

附言同样,与参数声明中的所有数组的情况一样,第一个 [] 总是无关紧要并且总是衰减为指针,这意味着以下也是上述 的有效等效原型声明sum2d

P.S. Again, as is the case with all arrays in parameter declarations, the first [] is always inconsequential and always decays to a pointer, meaning that the following are also valid equivalent prototype declarations for the above sum2d

    int sum2d(int, int, int a[][*]);
    int sum2d(int, int, int (*a)[*]);
    int sum2d(int n, int m, int (*a)[m]);

真正重要的是第二个[],必须声明为可变长度".

It is the second [] that really matters and has to be declared as "variable length".

这篇关于为什么使用星号“[*]"?而不是函数的 VLA 数组参数的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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