二维数组传递到功能 [英] 2D array passing to a function

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

问题描述

我一直在阅读本<一个href=\"http://stackoverflow.com/questions/6321670/passing-a-2d-array-in-a-function-in-c-program\">question但我不能够得到所产生的code来解决这个问题。
我应该如何才能使工作改变呢?

 无效print2(INT **数组,整数N,INT米);主要()
{
    int数组[] [4] = {{1,2,3,4},{5,6,7,8}};
    INT数组2 [] [2] = {{1,2},{3,4},{5,6},{7,8}};
    print2(数组,2,4);
}无效print2(INT **阵列,INT N,INT M)
{
    INT I,J;
    对于(i = 0; I&LT; N;我++)
    {
       为(J = 0; J&LT;米; J ++)
       的printf(%d个,数组[I] [J]);       的printf(\\ n);
    }
}


解决方案

您传递一个指向数组的指针,但你的函数需要一个指针的指针。 在C语言中,数组名衰减到那是指向第一个数组元素的值。在这种情况下,第一阵列元素是数组,所以函数参数衰变的指针数组。

下面是你能解决这个问题的方法之一。改变功能采取空隙* 使得尺寸不带参数干扰。然后,尺寸参数函数体中用于创建二维数组正确的指针类型。

 无效print2(无效* P,整数N,INT M)
{
    INT I,J;
    INT(*数组)[M] = P;
    对于(i = 0; I&LT; N;我++)
    {
       为(J = 0; J&LT;米; J ++)
          的printf(%d个,数组[I] [J]);
       的printf(\\ n);
    }
}

如果你愿意改变参数的顺序,那么你可以使用适当类型的数组参数:

 无效print2(INT N,INT男,int数组[N] [M])
{
    INT I,J;
    对于(i = 0; I&LT; N;我++)
    {
       为(J = 0; J&LT;米; J ++)
          的printf(%d个,数组[I] [J]);
       的printf(\\ n);
    }
}

由于杰克问C89,这里有一个方法来处理它。由于二维阵列是由相同的内存中的一个长一维数组,你可以走在指针传递这样。同样,我们接受输入参数作为无效* 来避免处理腐朽类型。然后,我们把指针作为一项长期的一维数组,但我们按照正确的尺寸走走吧:

 无效print2(无效* P,整数N,INT M)
{
    INT I,J;
    为int *阵列= P;
    对于(i = 0; I&LT; N;我++)
    {
       为(J = 0; J&LT;米; J ++)
          的printf(%d个,数组[I * M + J]);
       的printf(\\ n);
    }
}

I've been reading this question but I'm not able to get the resulting code to solve the problem. How should I change this in order to make it work?

void print2(int ** array,int n, int m);

main()
{
    int array[][4]={{1,2,3,4},{5,6,7,8}};
    int array2[][2]={{1,2},{3,4},{5,6},{7,8}};
    print2(array,2,4);
}

void print2(int ** array,int n,int m)
{
    int i,j;
    for(i=0;i<n;i++)
    {
       for(j=0;j<m;j++)
       printf("%d ",array[i][j]);

       printf("\n");
    }
}

解决方案

You are passing in a pointer to an array, but your function is expecting a pointer to a pointer. In C, the array name decays to a value that is the pointer to the first array element. In this case, the first array element is an array, so the function argument decays to a pointer to an array.

Here is one way you can fix this. Change the function to take a void * so that the dimension does not interfere with the argument. Then the dimension argument is used in the function body to create the proper pointer type for the 2D array.

void print2(void *p,int n,int m)
{
    int i,j;
    int (*array)[m] = p;
    for(i=0;i<n;i++)
    {
       for(j=0;j<m;j++)
          printf("%d ",array[i][j]);
       printf("\n");
    }
}

If you are willing to change the order of the arguments, then you can use the proper type for the array argument:

void print2(int n, int m, int array[n][m])
{
    int i,j;
    for(i=0;i<n;i++)
    {
       for(j=0;j<m;j++)
          printf("%d ",array[i][j]);
       printf("\n");
    }
}

Since Jack asked about C89, here's a way to handle it. Since a 2D array is organized the same as a long 1D array in memory, you can just walk the passed in pointer as such. Again, we accept the input parameter as a void * to avoid dealing with the decayed type. Then, we treat the pointer as a long 1D array, but we walk it according to the proper dimensions:

void print2(void *p, int n, int m)
{
    int i,j;
    int *array = p;
    for(i=0;i<n;i++)
    {
       for(j=0;j<m;j++)
          printf("%d ",array[i*m+j]);
       printf("\n");
    }
}

这篇关于二维数组传递到功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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