为什么我们在传递二维数组作为参数时需要指定列大小? [英] Why do we need to specify the column size when passing a 2D array as a parameter?
问题描述
为什么我的参数不能是
void example(int Array[][]){ /*statements*/}
为什么需要指定数组的列大小?比如说,3
Why do I need to specify the column size of the array? Say for example, 3
void example(int Array[][3]){/*statements*/}
我的教授说这是强制性的,但我在开学前就开始编码,我记得当我把它作为参数时没有语法或语义错误?还是我错过了什么?
My professor said its mandatory, but I was coding before school started and I remembered that there was no syntactical or semantic error when I made this my parameter? Or did I miss something?
推荐答案
在描述参数时,数组总是衰减为指向其第一个元素的指针.
When it comes to describing parameters, arrays always decay into pointers to their first element.
当您将声明为 int Array[3]
的数组传递给函数 void foo(int array[])
时,它会衰减为指向数组即int *Array;
.顺便说一句,您可以将参数描述为 int array[3]
或 int array[6]
甚至 int *array
- 所有这些都将是等效,您可以毫无问题地传递任何整数数组.
When you pass an array declared as int Array[3]
to the function void foo(int array[])
, it decays into a pointer to the beginning of the array i.e. int *Array;
. Btw, you can describe a parameter as int array[3]
or int array[6]
or even int *array
- all these will be equivalent and you can pass any integer array without problems.
在数组数组(二维数组)的情况下,它也会衰减到指向其第一个元素的指针,这恰好是一个一维数组,即我们得到 int (*Array)[3]代码>.
In case of arrays of arrays (2D arrays), it decays to a pointer to its first element as well, which happens to be a single dimensional array i.e. we get int (*Array)[3]
.
在此处指定大小很重要.例如,如果不是强制性的,编译器将无法知道如何处理表达式 Array[2][1]
.
Specifying the size here is important. If it were not mandatory, there won't be any way for compiler to know how to deal with expression Array[2][1]
, for example.
要取消引用编译器需要计算我们需要在连续内存块中的项目的偏移量(int Array[2][3]
是一个连续的整数块),这应该易于指针.如果a
是一个指针,则a[N]
扩展为start_address_in_a + N * size_of_item_being_pointed_by_a
.如果在函数内部使用表达式 Array[2][1]
(我们想要访问这个元素),Array
是一个指向一维数组和相同公式的指针适用.需要最后一个方括号中的字节数才能找到size_of_item_being_pointed_by_a
.如果我们只有 Array[][]
就不可能找到它,因此不可能解引用我们需要的数组元素.
To dereference that a compiler needs to compute the offset of the item we need in a contiguous block of memory (int Array[2][3]
is a contiguous block of integers), which should be easy for pointers. If a
is a pointer, then a[N]
is expanded as start_address_in_a + N * size_of_item_being_pointed_by_a
. In case of expression Array[2][1]
inside a function (we want to access this element) the Array
is a pointer to a single dimensional array and the same formula applies. The number of bytes in the last square bracket is required to find size_of_item_being_pointed_by_a
. If we had just Array[][]
it would be impossible to find it out and hence impossible to dereference an array element we need.
没有大小,指针算术将不适用于数组数组.Array + 2
会产生什么地址:将 Array
中的地址提前 2 个字节(错误)或将指针提前 3* sizeof(int) * 2代码> 提前字节?
Without the size, pointers arithmetics wouldn't work for arrays of arrays. What address would Array + 2
produce: advance the address in Array
2 bytes ahead (wrong) or advance the pointer 3* sizeof(int) * 2
bytes ahead?
这篇关于为什么我们在传递二维数组作为参数时需要指定列大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!