为什么我们需要指定的列大小传递一个二维数组作为参数是什么时候? [英] Why do we need to specify the column size when passing a 2D array as a parameter?

查看:161
本文介绍了为什么我们需要指定的列大小传递一个二维数组作为参数是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的参数

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数组[3] 给函数无效美孚(int数组[]),它衰变成一个指针数组的开头即为int *阵列; 。顺便说一句,你能描述一个参数为 int数组[3] int数组[6] 甚至为int *阵列 - 所有这些将是等效的,你可以通过任何整型数组没有问题

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(*阵列)[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].

指定大小这里是重要的。如果它不是强制性的,也不会有任何方式编译器知道如何处理与前pression 数组[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数组[2] [3] 是一个连续的块的整数),这应该是容易的指针。如果 A 是一个指针,那么 A [N] 被扩展为 start_address_in_a + N * size_of_item_being_pointed_by_a 。在EX pression的情况下,数组[2] [1] 函数(我们要访问这个元素)中的阵列是一个指向一维数组,同样的公式适用。需要在最后方括号中的字节数找到 size_of_item_being_pointed_by_a 。如果我们只有数组[] [] 这将是不可能找到它,因而无法提领我们需要一个数组元素。

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.

如果没有大小,指针算术不会为数组的数组工作。什么地址将阵列+ 2 农产品:在阵列提前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屋!

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