11

I am trying to pass a 2-d array to a function which accept a pointer to pointer. And I have learnt that a 2-d array is nothing a pointer to pointer(pointer to 1-D array). I when I compile the below code I got this error.

#include<iostream>

void myFuntion(int **array)
{
}
int main()
{
   int array[][]= {{1,2,3,4},{5,6,7,8,9},{10,11,12,13}};
   myFuntion(array);
   return 0;
}

In function 'int main()': Line 5: error: declaration of 'array' as multidimensional array must have bounds for all dimensions except the first compilation terminated due to -Wfatal-errors.

Can anybody clear my doubt regarding this and some docs if possible for my more doubts.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
yogi
  • 231
  • 2
  • 3
  • 8
  • 2
    Arrays are not pointers. It *isn't* a pointer to a pointer, and it *decays* to a pointer to an array when it's passed into a function. – chris Oct 20 '12 at 16:46
  • array[][] must have some size defined. I guess we are not allowed to specify an array without any size specified. – Afaq Oct 20 '12 at 16:49
  • rows should be mentioned!, in *array[3][] – Fatima Zohra Oct 20 '12 at 16:49
  • @FatimaZohra a pointer to a 3*x array? How did it get here? – John Dvorak Oct 20 '12 at 16:50
  • @FatimaZohra, Actually, you need to specify the other dimensions, not the first. Anyway, that's a 2D array of int pointers, and OP, your sets need to all contain the same number of elements so that you *can* specify the second dimension. – chris Oct 20 '12 at 16:51
  • @chris that waht I want to know Why I have to mention the size of in 2ne subscript of the 2-D array, line void myFunction (int array[][4]){}?? – yogi Oct 20 '12 at 16:54
  • [link] http://codepad.org/n8GRm9dU Error:- error: cannot convert 'int (*)[4]' to 'int**' for argument '1' to 'void myFuntion(int**)' why does this mean as I try to pass myFunction(array); – yogi Oct 20 '12 at 17:00
  • @ysbhai, Because it decays into a pointer to an array, not a pointer to a pointer. – chris Oct 20 '12 at 17:02
  • [related FAQ](http://stackoverflow.com/questions/4810664/) – fredoverflow Oct 20 '12 at 17:34

6 Answers6

8
  void myFunction(int arr[][4])

you can put any number in the first [] but the compiler will ignore it. When passing a vector as parameter you must specify all dimensions but the first one.

vmp
  • 2,370
  • 1
  • 13
  • 17
1

You should at least specify the size of your second dimension.

int array[][5] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13 } };

There is also an error which is often repeated. To pass a 2D array as argument, you have to use the following types:

void myFuntion(int (*array)[SIZE2]);
/* or */
void myFuntion(int array[SIZE1][SIZE2]);
md5
  • 23,373
  • 3
  • 44
  • 93
  • can you please give your review on this [link] codepad.org/n8GRm9dU [/link] Error:- error: cannot convert 'int ()[4]' to 'int*' for argument '1' to 'void myFuntion(int**)' why does this mean as I try to pass myFunction(array); – yogi Oct 20 '12 at 17:02
  • Can you give a logic behind this. void myFuntion(int (*array)[SIZE2]); – yogi Oct 20 '12 at 17:04
  • `array` is declared as a pointer to an array of size `SIZE2`. `SIZE2` is here the size of your second dimension. – md5 Oct 20 '12 at 17:05
  • Actually these both are same ---> int (*array)[SIZE2] = int array[SIZE1][SIZE2]).......................................... array[SIZE1][SIZE2] = ( *(array + SIZE1)[SIZE2] ) = ( *(array + 0)[SIZE2]) = *array[SIZE2]. Here 1st argument is ignored by compiler hence SIZE1 = 0. – Raj Kumar Mishra Oct 03 '17 at 08:15
1

Why don't use std::vector instead of "raw" arrays. Advantages:
1. It can dynamically grow.
2. There is no issues about passing arguments to the function. I.e. try to call void myFuntion(int array[SIZE1][SIZE2]); with array, that has some different sizes not SIZE1 and SIZE2

zabulus
  • 2,373
  • 3
  • 15
  • 27
1

Another templated solution would be:

template<int M, int N>
void myFunction(int array[N][M])
{
}
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0
#include<iostream>
 void myFuntion(int arr[3][4]);
int main()
  {
  int array[3][4]= {{1,2,3,4},{5,6,7,8},{10,11,12,13}};
 myFuntion(array);
  return 0;
 }
   void myFuntion(int arr[3][4])
   {

   }

http://liveworkspace.org/code/0ae51e7f931c39e4f54b1ca36441de4e

Fatima Zohra
  • 2,929
  • 2
  • 17
  • 17
0

declaration of ‘array’ as multidimensional array must have bounds for all dimensions except the first So you have to give

array[][size] //here you must to give size for 2nd or more 

For passing the array in function , array is not a pointer to a pointer but it's pointer to an array so you write like this

fun(int (*array)[])

Here if you miss the parenthesis around (*array) then it will be an array of pointers because of precedence of operators [] has higher precedence to *

Omkant
  • 9,018
  • 8
  • 39
  • 59