2

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

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].

Iharob Al Asimi
  • 51,091
  • 5
  • 53
  • 91
Athu
  • 29
  • 4
  • 1
    possible duplicate of [Passing array of character strings to function](http://stackoverflow.com/questions/3513481/passing-array-of-character-strings-to-function) – Santosh A Sep 21 '15 at 09:59
  • How is `Process` supposed to work if only 1 of the 2 dimensions of the array is passed? – John Coleman Sep 21 '15 at 10:44
  • 2
    [Do not cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). And most importantly never cast a variable just to silence the compiler. – Iharob Al Asimi Sep 21 '15 at 10:45
  • There is no 2D array! And do not cast, unless required and you are **really** know and accept **all** implications. – too honest for this site Sep 21 '15 at 10:52

3 Answers3

3

how can i access each value of pInput inside function 'Process'?I cannot access it directly as 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**.

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)

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

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

Community
  • 1
  • 1
legends2k
  • 27,643
  • 22
  • 108
  • 196
1

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

   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.   
  }
Sandeep Kumar
  • 306
  • 2
  • 9
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.

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 (*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.
  • 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.
  • The correct form of main is int main (void).
  • Casting the result of malloc is pointless.
  • Always check the result of malloc and remember to clean up allocated data.
Lundin
  • 155,020
  • 33
  • 213
  • 341