Passing a 2-dimensional array as an argument

I am trying to pass a 2 dimensional array to a function that takes a pointer to a pointer. And I found out that the 2nd array is not a pointer to a pointer (a pointer to a 1-D array). When I compile the code below, 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 the 'int main ()' function: Line 5: error: declaring the array as a multidimensional array must have boundaries for all dimensions except the first compilation is completed due to -Wfatal-errors.

Someone can explain my doubts about this and some documents, if possible, for my big doubts.

+4
source share
5 answers
  void myFunction(int arr[][4]) 

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

+2
source

You must at least indicate 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 a mistake that often repeats. To pass a 2D array as an argument, you must use the following types:

 void myFuntion(int (*array)[SIZE2]); /* or */ void myFuntion(int array[SIZE1][SIZE2]); 
0
source

Why not use std :: vector instead of "raw" arrays. Benefits:
1. It can grow dynamically.
2. There is no problem passing arguments to a function. That is, try calling void myFuntion (int array [SIZE1] [SIZE2]); with an array that has different sizes, not SIZE1 and SIZE2

0
source
 #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

0
source

Declaring an array as a multidimensional array must have boundaries for all dimensions except the first. Therefore, you must give

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

To pass an array to a function, an array is not a pointer to a pointer, but it points to an array, so you write like this

 fun(int (*array)[]) 

Here, if you skip the parenthesis around (* array), then it will be an array of pointers due to the priority of the operators [] has a higher priority on *

0
source

Source: https://habr.com/ru/post/1440985/


All Articles