3

I'm getting this warning and scratching my head about why. I found many thread here that address this warning in VS 2017 compiler, but not this particular combination: why isn't int** the same level of indirection as int[X][Y]?

Here's a distilled down example that generates the warning:

void testfunc(int ** input, int ** output) {

  /*
  * Do something
  */

}


int main()
{


  int   firstvar[2][4];
  int   secondvar[2][4];

  testfunc(firstvar, secondvar);

}

I get:

testcode.c(46): warning C4047: 'function': 'int **' differs in levels of indirection from 'int [2][4]'

Any thoughts on why this would be or how to fix it much appreciated.

DMK
  • 53
  • 4
  • 2
    `int**` is *not* a 2D array. You cannot access 2D array elements using such a pointer. – Eugene Sh. Jan 28 '19 at 17:37
  • 1
    Arrays aren't pointers. Arrays *decay* into pointers, but that doesn't mean that a 2D array can decay into a pointer-to-pointer. http://c-faq.com/aryptr/index.html – jamesdlin Jan 28 '19 at 17:38

1 Answers1

2
void testfunc(int input[][4], int output[][4])

would be a better way to pass this. Please note int ** seems to indicate it is an array of int *, which it is not.

void onedimension_testfunc(int *odinput, int *odoutput)
{
     ...
}

int main ()
{
      int odfirst[4], odsecond[4];
      onedimention_testfunc(odfirst, odsecond);
}

In one dimension the above code works fine. Thats because odinput/odoutput in the above code points to an integer array. But not for multiple dimensions.

user2962885
  • 111
  • 2
  • 10
  • And it worth noting that the actual type passed would be `int (*input)[4]` – Eugene Sh. Jan 28 '19 at 17:40
  • Thanks for the reply. The reason I didn't pass it as int[][4], though, is that the actual dimensions will be variable. I think I will need to redesign this as a single dimension structure and pass in also the magnitude of the first dimension, so that it can be accessed as rowsize*r +c – DMK Jan 28 '19 at 19:32
  • You don't have to use one-dimensional arrays if your data is two dimensional. Just use variable sized 2d-arrays as parameters like this: `void testfunc(size_t n, size_t m, int input[n][m], int output[n][m])` And call it like this: `testfunc(2, 4, firstvar, secondvar);` – HAL9000 Jan 29 '19 at 08:46