为什么不能将作为输入参数传递给函数的2D数组在其函数声明中声明为int **? [英] Why can't a 2D array passed as input argument to a function cannot be declared as int** in its function declaration?

查看:72
本文介绍了为什么不能将作为输入参数传递给函数的2D数组在其函数声明中声明为int **?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个数组 arr 一个1D数组和 brr 一个2D数组,我想将这些数组作为输入传递给函数

如果是一维,则:

void fun(int *); void fun(int []);//允许两个声明

  fun(arr);//调用一维无效的乐趣(int * ptr){//一维功能定义} 

但是在2D阵列的情况下:

  void fun(int [] []);//允许无效的乐趣(int **);//这是不允许的**为什么这样** 

虽然调用和定义与先前的1D数组相似

  fun(brr);//调用无效的乐趣(int ** ptr2){//代码} 

解决方案

这是因为按照惯例,在很多情况下,尤其是在函数传递的参数上,指向数组的指针被视为指向类型的第一个元素的合格指针.>

您可以查阅 ISO/IEC 9899:2011 § 6.7.6.3函数声明符(包括原型)-语义:

将参数声明为类型数组"应调整为类型的合格指针",其中类型限定符(如果有)是在数组类型派生的[和]中指定的那些.如果关键字static也出现在数组类型的[和]中推导,然后对于函数的每次调用,相应的实际参数应提供对第一个参数的访问数组的元素,其元素的数量至少与大小表达式.

但是类型为 int [] [] 而不是 int ** 的声明是不可互换的,因为虽然第一个引用的是2D数组,但它会静默地编译器将其更改为指向2D数组第一个元素的指针,第二个则表示指向int的指针的指针,该指针与2D数组不兼容.

有时候,人们会被如下动态定义的伪2D数组弄得一团糟:

  int ** My_2D_array = malloc(sizeof(int *)* 2);//我们正在分配2个指向int的指针My_2D_array [0] = malloc(sizeof(int)* 2);//我们分配第一维空间My_2D_array [1] = malloc(sizeof(int)* 2);//我们分配第二维空间 

在这种情况下,名称 My_2D_array 将被称为由2个指向int的指针组成的数组的第一个元素的地址,并使用 [] 运算符将返回指针数组中的第一个指针.再次将运算符 [] 应用于刚刚获得的指针,并再次用作int的一维数组的第一个元素的地址,将重现一个int:

  int my_int = My_2D_array [0] [1];//模仿一个二维数组,但是不是! 

If suppose I have two arrays arr a 1D array and brr a 2D array and I want to pass these arrays as input to functions

In case of a 1D :

void fun(int*); OR void fun(int[]); //both declarations are allowed

fun(arr);//call for 1D

void fun(int *ptr)
{
 //function def of 1D
}

But in case of a 2D Array:

void fun(int[][]); //allowed
void fun(int**);  //this is not allowed **WHY so?**

While the call and definitions are similar to the previous 1D array

fun(brr);//call

void fun(int **ptr2)
{
 //code
}

解决方案

This is because by convention a pointer to an array is considered as a qualified pointer to the first element of type in many occasions especially on function passed parameters.

You can consult the ISO/IEC 9899:2011 § 6.7.6.3 Function declarators (including prototypes) - Semantics:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.

But a declaration of type int[][] instead of int**is not interchangeable because while the first refers to a 2D array, and it will be silently changed by the compiler to a pointer to the first element of the 2D array, the second means a pointer to pointer of int, that is not compatible with a 2D array.

Sometime people are induced in confusion by pseudo 2D arrays defined dynamically as follows:

int **My_2D_array = malloc(sizeof(int *) * 2);  //We are allocating 2 pointers to int
My_2D_array[0] = malloc(sizeof(int)*2);  //We allocate the first dimension space
My_2D_array[1] = malloc(sizeof(int)*2);  //We allocate the second dimension space

In this case the name My_2D_array will be referred as the address of the first element of an array of 2 pointers to int, and the use of the [] operator will return the first pointer in the pointers array. Applying again the operator [] onto the pointer just obtained, and again used as the address of the first element of a monodimensional array of int's, will returs an int:

int my_int = My_2D_array[0][1];  //mimics a bidimensional array, but isn't!

这篇关于为什么不能将作为输入参数传递给函数的2D数组在其函数声明中声明为int **?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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