在数组参数数组的长度 [英] Array lengths in array parameters

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

问题描述

我读的 C程序设计:一种现代方法的由K.N.King学习的C编程语言和当前章讲述功能,同时还数组参数。据解释说,人们可以使用结构,如这些给前preSS的参数数组的长度:

1

 无效MYFUNC(INT A,INT B,INT [A],INT [B],INT [*]); / * *原型/无效MYFUNC(INT A,INT B,诠释N [A],INT M [B],INT C [A + B + other_func()){
... /* 身体 */
}

2

 无效MYFUNC(int [静态5]); / * *原型/无效MYFUNC(INT一个[静态5]){
... /* 身体 */
}

所以,问题(S)是:

是例1中的结构纯粹化妆品或做他们对编译器产生影响?

是在这种情况下只有美容性质静态修改?到底是什么意思,做?

C 是否也可以宣布这样一个数组参数。并且它是化妆品与例1是什么?

 无效MYFUNC(INT [4]);无效MYFUNC(int类型的[4]){...}


解决方案

功能参数数组最里面的尺寸总是重写为指针,以便你给出的值有没有太多的意义,很遗憾。这改变了多维数组:从第二个层面开始这些然后由编译器计算之类的东西 A [i] [j]的

静态在这方面意味着调用者至少提供尽可能多的元素。大多数编译器忽略本身的价值。最近的一些编译器从中推断出一个空指针是不允许作为参数,并警告你,如果相应的可能。

此外观察,原型可能有 * 这么清楚的价值并不重要存在。在多维数组的情况下,具体的值是一个与前任pression的定义来计算。

I am reading C Programming: A Modern Approach by K.N.King to learn the C programing language and the current chapter tells about functions, and also array parameters. It is explained that one can use constructs like these to express the length of array parameters:

1.

void myfunc(int a, int b, int[a], int[b], int[*]); /* prototype */

void myfunc(int a, int b, int n[a], int m[b], int c[a+b+other_func()]) {
... /* body */
}

2.

void myfunc(int[static 5]); /* prototype */

void myfunc(int a[static 5]) {
... /* body */
}

So the question(s) are:

a. Are the constructs in example 1 purely cosmetic or do they have an effect on the compiler?

b. Is the static modifier in this context only of cosmetic nature? what exactly does it mean and do?

c. Is it also possible to declare an array parameter like this; and is it as cosmetic as example 1 is?

void myfunc(int[4]);

void myfunc(int a[4]) { ... }

解决方案

The innermost dimension of function array parameters is always rewritten to a pointer, so the values that you give there don't have much importance, unfortunately. This changes for multidimensional arrays: starting from the second dimension these are then used by the compiler to compute things like A[i][j].

The static in that context means that a caller has to provide at least as many elements. Most compilers ignore the value itself. Some recent compilers deduce from it that a null pointer is not allowed as an argument and warn you accordingly, if possible.

Also observe that the prototype may have * so clearly the value isn't important there. In case of multidimensional arrays the concrete value is the one computed with the expression for the definition.

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

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