在函数中处理一个二维数组

时间:2021-10-23 21:23:56

I have a function which accepts int* pInput[] as an argument.

我有一个函数,它接受int* pInput[]作为参数。

void Process(int*  pInput[], unsigned int num);

I have to call this function via 2 methods as

我必须通过两个方法调用这个函数

main()
{
int *pIn[2];
int input[2][100] = {0};

pIn[0] = ( int* )malloc( 100 * sizeof( int) );
pIn[1] = ( int* )malloc( 100 * sizeof( int) );

Process( pIn, 2 );
Process(  ( int** )input, 2 );
}

Then how can i access each value of pInput inside function 'Process'?I cannot access it directly as pIn[0][0].

那么我如何访问pInput inside function 'Process'的每个值呢?我不能直接访问它作为pIn[0][0]。

3 个解决方案

#1


3  

how can i access each value of pInput inside function 'Process'?I cannot access it directly as pIn[0][0].

如何在函数“进程”中访问pInput的每个值?我不能直接访问它作为pIn[0][0]。

No! You can access it exactly that way: pInput[0][0] if the input you pass is pIn. This is because pIn is an array of int*s I.e. it's of type int *[n] each of its element pointing to an array of ints. It would decay into int**.

不!您可以这样访问它:如果您传递的输入是pIn,那么pInput[0][0]。这是因为pIn是一个int*s的数组,也就是说它的类型是int* [n],每个元素都指向一个ints数组。它会衰变为int**。

However, if you want to pass input, a 2D array of ints, you've to do more since a 2D array doesn't decay into a double pointer, T** but into a pointer to an array, T (*) [n]. This is because array decay is not recursive, it happens only to the first level. Alternatively, you can do this (Live example)

但是,如果你想要传递输入,一个2D数组的ints,你需要做更多的事情,因为2D数组不会衰变为双指针,T**,而是指向一个数组的指针,T (*) [n]。这是因为数组衰减不是递归的,它只发生在第一级。或者,您也可以这样做(活动示例)

pIn[0] = input[0];
pIn[1] = input[1];

and now pass pIn to Process. Here pIn is a surrogate for input and it needs to have as many elements as input, which is not a very elegant solution. A better way to pass input, when you know the dimensions during compile-time is

现在将引脚传递给进程。这里的pIn是输入的代理,它需要有和输入一样多的元素,这不是一个很好的解决方案。当您知道编译时的维度时,一个更好的传递输入的方法。

void Process(int (*pInput)[100], size_t rows) { }
void Process(int input [2][100], size_t rows) { }
/* These two are the same; the compiler never sees the `2`. input's type is int(*)[100] */

Read on array decay to understand the situation better.

读取数组衰减,以更好地理解情况。

Aside

Related

#2


1  

In your process() function you just need to access it normally like any 2d array as below. Calling both ways are same.

在您的进程()函数中,您只需要像下面这样访问它通常的2d数组。调用两种方法都是一样的。

   void Process( int * pInput[], unsigned int num)
   {
       printf(" %d", pInput[0][0]); //printing value of pInput[0]   
       printf(" %d", pInput[1][0]); //printing value of pInput[1]   
       pInput[0][0] = 8054;         // changing its value.
       pInput[1][0] = 8055;         // changing its value.
   }


int main()
{
  int *pIn[2];
  int input[2][100] = {0};

  pIn[0] = ( int* )malloc( 100 * sizeof( int) );

  pIn[1] = ( int* )malloc( 100 * sizeof( int) );


  // assigning value to array.
  pIn[0][0] = 23;
  pIn[0][1] = 2;

  pIn[1][0] = 5689;
  pIn[1][1] = 5643;

  Process( pIn, 2 ); //calling process funtion
  printf(" %d", pIn[1][0]);  //printing the changed value by process funtion.   
  }

#3


0  

You are getting confused because you are using different types when there's no need for such. Arrays follow the same rules of indirection as any other type. If you would allocate a plain int dynamically, you would write int* x = malloc(sizeof(*x));. Simply do the very same thing when it comes to arrays. Don't confuse things by mixing in the "arrays decay to pointers" rule.

您会感到困惑,因为您正在使用不同的类型,而不需要这样。数组遵循与其他类型相同的间接规则。如果您要动态地分配一个普通int类型,您将写入int* x = malloc(sizeof(*x));当涉及到数组时,只需做同样的事情。不要混淆“数组衰减到指针”规则。

So we have int input[2][100], very straight-forward, it is a plain 2D array. Now if you want to allocate that dynamically, you will need a pointer to such an array:

我们有int输入[2][100],非常直接,它是一个普通的二维数组。如果你想动态分配,你需要一个指针指向这样一个数组:

int (*pIn)[2][100]; // pointer to an array of int [2][100].

pIn = malloc(sizeof(*pIn));

And the whole program would then be:

整个项目会是:

#include <stdlib.h>

void Process (size_t num, int pInput[num][100])
{
}

int main (void)
{
  int (*pIn)[2][100];
  int input[2][100] = {0};

  pIn = malloc(sizeof(*pIn));
  if(pIn == NULL)
  {
    // error handling
    return 0;
  }

  Process(2, *pIn);
  Process(2, input);

  free(pIn);
  return 0;
}

Comments:

评论:

  • size_t is the most correct type to use for array sizes, as it is the type returned by the sizeof operator. So it is just an unsigned integer with a fancy name.
  • size_t是最适合数组大小的类型,因为它是由sizeof运算符返回的类型。所以它只是一个无符号整数,有一个奇特的名字。
  • int pInput[num][100] in the function will actually decay into an array pointer to an array of 100 int. You don't need to know that to use it though, simply use pInput[x][y] and pretend it is a 2D array. The important thing here is to understand that the array is not passed by value.
  • 函数中的int pInput[num][100]实际上会衰减成一个指向100 int的数组的数组指针。这里重要的是要理解数组不是通过值传递的。
  • The correct form of main is int main (void).
  • main的正确形式是int main (void)。
  • Casting the result of malloc is pointless.
  • 使用malloc的结果是没有意义的。
  • Always check the result of malloc and remember to clean up allocated data.
  • 始终检查malloc的结果,并记住清理已分配的数据。

#1


3  

how can i access each value of pInput inside function 'Process'?I cannot access it directly as pIn[0][0].

如何在函数“进程”中访问pInput的每个值?我不能直接访问它作为pIn[0][0]。

No! You can access it exactly that way: pInput[0][0] if the input you pass is pIn. This is because pIn is an array of int*s I.e. it's of type int *[n] each of its element pointing to an array of ints. It would decay into int**.

不!您可以这样访问它:如果您传递的输入是pIn,那么pInput[0][0]。这是因为pIn是一个int*s的数组,也就是说它的类型是int* [n],每个元素都指向一个ints数组。它会衰变为int**。

However, if you want to pass input, a 2D array of ints, you've to do more since a 2D array doesn't decay into a double pointer, T** but into a pointer to an array, T (*) [n]. This is because array decay is not recursive, it happens only to the first level. Alternatively, you can do this (Live example)

但是,如果你想要传递输入,一个2D数组的ints,你需要做更多的事情,因为2D数组不会衰变为双指针,T**,而是指向一个数组的指针,T (*) [n]。这是因为数组衰减不是递归的,它只发生在第一级。或者,您也可以这样做(活动示例)

pIn[0] = input[0];
pIn[1] = input[1];

and now pass pIn to Process. Here pIn is a surrogate for input and it needs to have as many elements as input, which is not a very elegant solution. A better way to pass input, when you know the dimensions during compile-time is

现在将引脚传递给进程。这里的pIn是输入的代理,它需要有和输入一样多的元素,这不是一个很好的解决方案。当您知道编译时的维度时,一个更好的传递输入的方法。

void Process(int (*pInput)[100], size_t rows) { }
void Process(int input [2][100], size_t rows) { }
/* These two are the same; the compiler never sees the `2`. input's type is int(*)[100] */

Read on array decay to understand the situation better.

读取数组衰减,以更好地理解情况。

Aside

Related

#2


1  

In your process() function you just need to access it normally like any 2d array as below. Calling both ways are same.

在您的进程()函数中,您只需要像下面这样访问它通常的2d数组。调用两种方法都是一样的。

   void Process( int * pInput[], unsigned int num)
   {
       printf(" %d", pInput[0][0]); //printing value of pInput[0]   
       printf(" %d", pInput[1][0]); //printing value of pInput[1]   
       pInput[0][0] = 8054;         // changing its value.
       pInput[1][0] = 8055;         // changing its value.
   }


int main()
{
  int *pIn[2];
  int input[2][100] = {0};

  pIn[0] = ( int* )malloc( 100 * sizeof( int) );

  pIn[1] = ( int* )malloc( 100 * sizeof( int) );


  // assigning value to array.
  pIn[0][0] = 23;
  pIn[0][1] = 2;

  pIn[1][0] = 5689;
  pIn[1][1] = 5643;

  Process( pIn, 2 ); //calling process funtion
  printf(" %d", pIn[1][0]);  //printing the changed value by process funtion.   
  }

#3


0  

You are getting confused because you are using different types when there's no need for such. Arrays follow the same rules of indirection as any other type. If you would allocate a plain int dynamically, you would write int* x = malloc(sizeof(*x));. Simply do the very same thing when it comes to arrays. Don't confuse things by mixing in the "arrays decay to pointers" rule.

您会感到困惑,因为您正在使用不同的类型,而不需要这样。数组遵循与其他类型相同的间接规则。如果您要动态地分配一个普通int类型,您将写入int* x = malloc(sizeof(*x));当涉及到数组时,只需做同样的事情。不要混淆“数组衰减到指针”规则。

So we have int input[2][100], very straight-forward, it is a plain 2D array. Now if you want to allocate that dynamically, you will need a pointer to such an array:

我们有int输入[2][100],非常直接,它是一个普通的二维数组。如果你想动态分配,你需要一个指针指向这样一个数组:

int (*pIn)[2][100]; // pointer to an array of int [2][100].

pIn = malloc(sizeof(*pIn));

And the whole program would then be:

整个项目会是:

#include <stdlib.h>

void Process (size_t num, int pInput[num][100])
{
}

int main (void)
{
  int (*pIn)[2][100];
  int input[2][100] = {0};

  pIn = malloc(sizeof(*pIn));
  if(pIn == NULL)
  {
    // error handling
    return 0;
  }

  Process(2, *pIn);
  Process(2, input);

  free(pIn);
  return 0;
}

Comments:

评论:

  • size_t is the most correct type to use for array sizes, as it is the type returned by the sizeof operator. So it is just an unsigned integer with a fancy name.
  • size_t是最适合数组大小的类型,因为它是由sizeof运算符返回的类型。所以它只是一个无符号整数,有一个奇特的名字。
  • int pInput[num][100] in the function will actually decay into an array pointer to an array of 100 int. You don't need to know that to use it though, simply use pInput[x][y] and pretend it is a 2D array. The important thing here is to understand that the array is not passed by value.
  • 函数中的int pInput[num][100]实际上会衰减成一个指向100 int的数组的数组指针。这里重要的是要理解数组不是通过值传递的。
  • The correct form of main is int main (void).
  • main的正确形式是int main (void)。
  • Casting the result of malloc is pointless.
  • 使用malloc的结果是没有意义的。
  • Always check the result of malloc and remember to clean up allocated data.
  • 始终检查malloc的结果,并记住清理已分配的数据。