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.

9
  • 3
    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. Commented Oct 20, 2012 at 16:46
  • array[][] must have some size defined. I guess we are not allowed to specify an array without any size specified. Commented Oct 20, 2012 at 16:49
  • rows should be mentioned!, in *array[3][] Commented Oct 20, 2012 at 16:49
  • @FatimaZohra a pointer to a 3*x array? How did it get here? Commented Oct 20, 2012 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. Commented Oct 20, 2012 at 16:51

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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]);

4 Comments

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);
Can you give a logic behind this. void myFuntion(int (*array)[SIZE2]);
array is declared as a pointer to an array of size SIZE2. SIZE2 is here the size of your second dimension.
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.
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

Comments

1

Another templated solution would be:

template<int M, int N>
void myFunction(int array[N][M])
{
}

Comments

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

Comments

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 *

Comments

0

you need to pass array[][n] like this. you can leave 1st bracket but you have to put data into second.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.