0

so I'm working on this code,in which I'm trying to pass a two dimensional array to a function ,like so:

function signature : void f(array[4][4],int n);

int array [4][4];
f(&array[0][0],16)
for (int i=0;i<n;i++) // I'm working by c99 standard.
{
 hist[ *(&array[0][0] +i) ] ++ ;  // I know the max value in array[4][4] , and hist is initialized accordingly.
}
}

I just keep getting all kinds of errors regarding incompatible types , for example , I get "expected (*)[4] but type is of int * help ? :)

Caesar23
  • 93
  • 7

1 Answers1

0

&array[0][0] is an int pointer.

A pointer can't be converted to a two-dimensional array. For this reason, you need to pass it like f(array, 16). Basically you're passing an int* and that is incompatible with an int[][4].

It's worth pointing out that this is valid for one dimensional arrays (though it's unnecessary and potentially confusing):

void g(int x[]) {}
int arr[4];
g(arr); //valid of course
g(&arr[0]); //also valid

This explains why it's different with multi-dimensional arrays.

Community
  • 1
  • 1
Corbin
  • 33,060
  • 6
  • 68
  • 78
  • is this syntax valid ? `f (array,n) { for (int i=0;i – Caesar23 Jan 10 '13 at 01:51
  • @user1965208 `f (array,n)` is an invalid function signature. Also, if array is a 2d array, you should just use `array[i][j]`. The `*(*(array+i)+j)` is indeed correct--at least syntactically--though. – Corbin Jan 10 '13 at 02:08
  • actually I've already declared the function's signature in the first comment, and secondly , in hindsight I realized that you're correct, it may have been easier to use brackets , but this mistake has benefited me because I've learned so much about multi-dimensional arrays in the past hour or so. Anyway , thank you for your replies :) – Caesar23 Jan 10 '13 at 02:31