C参数数组声明 [英] C Parameter Array Declarators

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

问题描述

在C99有变长数组,也可以是静态的预选赛(和类型限定符)在参数组声明:

In C99 there are variable-length arrays, and there can be static qualifiers (and type qualifiers) in parameter array declarators:

void f(int i, int *a);
void f(int i, int a[]);
void f(int i, int a[i]);
void f(int i, int a[*]);         // Only allowed in function prototypes.
void f(int i, int a[static i]);

由于阵列功能参数只是衰减到指针,有没有previous声明之间的任何实际差别,或者是一个风格问题?当应任何人使用?特别是,什么是静态预选赛意味着什么呢?该标准不渲染以及明确每个语法的原因。

Since array function parameters simply decay to pointers, is there any practical difference between the previous declarations, or is it a matter of style? When should any of them be used? In particular, what does the static qualifier imply? The standard does not render well clear the reason for each syntax.

推荐答案

只要你用一维数组工作而已,上面的声明都是等效的。最后一个,虽然

As long as you are working with single-dimensional arrays only, the above declarations are all equivalent. The last one though

void f(int i, int a[static i])

有一个额外的效果。它等同于previous那些在参数类型而言,还告诉它可以依靠 A 参数指向的至少一个阵列,编译器 I 元素(可以在优化使用)。

has an extra effect. It is equivalent to the previous ones in terms of the parameter types, but also tells the compiler that it can rely on a parameter pointing to an array of at least i elements (which can be used in optimizations).

您也忘了另一个新的声明

You are also forgetting another new declaration

void f(int i, int a[const])

此人真正确实有,即使在一维阵列的情况下的效果。它相当于

This one actually does have an effect even in case of a single-dimensional array. It is equivalent to

void f(int i, int *const a)

虽然会有一些争论在功能参数常量,学历都是没用的。这是不可能的前常量,限定指针数组参数衰减使用 [] 语法声明时。

* (以及 I )之间的 [] 开始,只有当它是多维数组声明第二个(或以上)对 [] 之间用于重要。从本质上讲,它就像它一直是:在参数声明数组的大小总是要紧只是第二或进一步对之间的[] 。在 * 在原型声明的VLA参数使用,当值的大小没有明确命名。例如,你可以声明

The * (as well as i) between the [] begins to matter only when it is used between the second (or greater) pair of [] in multi-dimensional array declaration. In essence, it is just like it has always been: array size in the parameter declaration always mattered only between the second or further pair of []. The * is used in prototype declarations for VLA parameters, when the size value is not named explicitly. For example, you can declare

void bar(int n, int m, int a[n][m]);

和编译器会知道 A 是一个VLA,因为大小不常量。但是,如果你preFER不来命名原型参数,你打算怎么告诉的编译器是一个VLA?这时候, * 帮助

and the compiler will know that a is a VLA since the sizes are not constants. But if you prefer not to name parameters in prototypes, how are you going to tell the compiler that a is a VLA? That's when * helps

void bar(int, int, int a[*][*]);

这篇关于C参数数组声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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