0

When I create a 2D array, I think the variable is just a pointer to the first element of the array. So I feel I should just be able to pass it straight to a function that expects a pointer as a parameter. It seems I can do this for a one dimensional array but not for multidimensional. Can someone explain why this is?

void the_function(int* the_array){}

int main(){
   int the_array[3][3] = {{1,2,3},{1,2,3},{1,2,3}};

   the_function(the_array);
}

If I try and compile this I get the following error:

candidate function not viable: no known conversion from 'int [3][3]' to 'int *' for 1st argument
void the_function(int* the_array){}
     ^

But it works if I instead use a single dimension for the_array.

I can make it work if I put a * before the argument in my function call like this: void the_function(int* the_array){}

int main(){
   int the_array[3][3] = {{1,2,3},{1,2,3},{1,2,3}};

   the_function(*the_array);
}

But I feel like that * would be dereferencing the pointer, which I don't want to do. It should still be a pointer when passed to the function.

Any ideas?

Sam
  • 1,057
  • 5
  • 16
  • 20

0 Answers0