0

I am passing 2d array to a function, But its giving me error that passing argument 1 of ‘jac’ from incompatible pointer type

this is how I created the array

double t[m+2][n+2];
....
jac(t);
printf("*should print what assigned in jac function %f\n",t[0][0]);

in called function I am doing like this

void jac(double **tnew)
{
   tnew[0][0]=tnew[2][1]+tnew[0][1];

} 

why its giving me warning that passing argument 1 of ‘jac’ from incompatible pointer type

user786
  • 3,902
  • 4
  • 40
  • 72
  • I need to know if I have declared variable like this `double t[m+2][n+2];` then how to pass it to function. best would be if parameter signature is some pointer – user786 Dec 12 '21 at 06:57
  • You can use `void jac(int m, int n, double tnew[m+2][n+2])`. NOTE: `t` is a *Variable Length Array* so you will need to provide at minimum the dimension for the last `[..]`. Your array has type `double (*)[n+2]` (pointer-to-array-of `double[n+2]`) Not type `double **` (pointer-to-pointer-to `double`) Also type `"[c] passing 2d array"` into the search box above. – David C. Rankin Dec 12 '21 at 08:16
  • @DavidC.Rankin what variable size array means. Can u please explain I simple words – user786 Dec 12 '21 at 09:15
  • `double **` and `double [m+2][n+2]` are two different, incompatible types. – n. m. could be an AI Dec 12 '21 at 09:18
  • What about double[][] and (*)[] are they compatible? – user786 Dec 12 '21 at 09:34
  • There is no such thing as `double[][]`, you need an actual size in there (at least in the rightmost brackets). – n. m. could be an AI Dec 12 '21 at 20:32
  • An array, on access, is converted to a pointer to the first element of the array. In C, a 2D array is an *array of arrays* (e.g. a 1D array of arrays). Declaring `double t[x][y]` declares a 2D VLA. On access `t` is converted to a pointer to the first element of `t` (which is another 1D array of size `y` here). So the type is `double (*)[y]`. You can also write the type without applying the conversion to pointer as `double [x][y]`. You can use either. You can omit `x`, but must provide the final dimension `y`, so `double[][y]` is also fine. None of which is `double **`. – David C. Rankin Dec 13 '21 at 00:32

0 Answers0