用多维数组参数表示函数的方式 [英] Ways to represent function with multi-dimensional array parameters

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

问题描述

我目前正在学习在C语言中使用多维数组的函数.

I'm currently learning function that work with multi-dimensional array in C.

假设我有

short myArray[10][30].

我已经声明以下函数原型可以与myArray一起使用.

I have declared following function prototype to work with myArray.

void first(int n, short ar[][30]);
void second(int n, short ar[30]);
void third(int n, short (*ar)[30]);

我的理解是firstthird函数中的ar参数是等效的. first函数中的short ar[][30]等同于third函数中的short (*ar)[30],因为它们都是指向array of 30 short的指针.

My understanding is that the ar parameter in both first and third function are equivalent. short ar[][30] in first function is equivalent to the short (*ar)[30] in third function because they are both pointer to array of 30 short.

second函数中的ar是不同的,因为在short ar[30]中,arpointer to short,而不是指向array of 30 short的指针.

The ar in second function is different because in short ar[30], ar is a pointer to short, not a pointer to array of 30 short.

我的理解正确吗?

推荐答案

是的,您的理解是正确的. 1和3是等效的.并且2也是正确的(但不适用于传递2d数组-传递1D数组是正确的).但是,第二种情况会有所澄清.

Yes your understanding is correct. 1 and 3 are equivalent. And 2 is also right (But not for passing 2d array - it is correct for passing 1D array). But will clarify a bit the second case.

第二个编译器不考虑第三个括号内的内容.您可以忽略它,但编译器不会抱怨. 实际上,您在这里传递了一个short 的一维数组,该数组递减为指向第一个元素的指针(第一个元素为short它是short*).所以第二个也可以写为short *ar.

And the second one that 30 inside of third brackets are not considered by the compiler. You can omit it still the compiler won't complain. Actually here you have passed an 1D array of short that decayed into pointer to the first element (First element being short it is short*). So the second one you can also write as short *ar.

void second(int n, short ar[]);
void second(int n, short* ar );

这两个作品在这种情况下是等效的. 第二个用于传递一维数组,例如

These two works and they are equivalent in this context. The second one is for passing 1D array something like

   second(n, myArray[5]);

问题是,大多数时间数组会衰减为指针(sizeof运算符或Alignof等除外).将数组传递给函数是数组衰减的情况.

The thing is, most of the time array decays into pointer (exception is sizeof operator or Alignof etc). Passing an array to a function is a case where the array decays.

还传递了int数组,因此写short是错误的.(intshort可能具有相同的大小,但可以保证int的大小大于或等于short的大小).如果您使用short,然后在可能有效的声明中写了int.

Also you are passing int arrays so it is wrong to write short.(int and short may have same size but it is guaranteed that size of int would be larger than or equal to the size of short). If you used short and then wrote int in the declaration that would have worked.

编辑:第二个不用于传递2d数组.让我们清楚一点.您不能将2d数组传递给原型声明为第二个的函数.对于指针,有两件事要考虑-它的类型和它的值.如果您试图将2d数组传递给相同的函数,那将是非法的. 2d数组衰减为int (*)[30],它与int *int[]完全不同.

Edit: The second one is not for passing 2d array. Let's be clear on that. You can't pass 2d array to a function with the prototype declared as the second one. For pointers there are 2 things to consider - it's type and it's value. If you tried to pass a 2d array to the same function that would be illegal. 2d array decays into int (*)[30] which is not in anyway same as int * or int[].

这篇关于用多维数组参数表示函数的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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