0

Good morning to everyone, i know there is so many forms to pass an Array in to a function, and there are so many potst about this. But even reading this post i can not pass my array to the function. When I call the function on main, it appears an error as it shows on the following code.

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int row = 5;
int column=20;
void dimensions (int [20]);

int main (){

int *matriuprova[column];
for (int i= 0; i<column;i++){

    matriuprova[i] = new int [row];

    dimensions(matriuprova);// <-- here is the error:
        //main.cpp:14:31: error: cannot convert 'int**' to 'int*' 
        //for argument '1' to 'void dimensions(int*)'

}//end of for loop

return 0;
}//end of main

void dimensions (int *matriuprova [20]){
        //function code
         }

I have followed this link: Passing a 2D array to a C++ function

Thanks to everyone!

Community
  • 1
  • 1
Aleix Rius
  • 32
  • 1
  • 6

2 Answers2

2

You have wrong forward declaration of dimensions.

Change

    void dimensions (int [20]);

to

    void dimensions (int* [20]);
Jakiša Tomić
  • 280
  • 1
  • 7
  • 1
    And note that `void dimensions (int* [20]);` is the same as `void dimensions (int**);`. The latter is less confusing. – juanchopanza Jul 07 '15 at 09:16
  • @juanchopanza I don't think an array of pointers is the same as a pointer to a pointer... – jiggunjer Jul 07 '15 at 10:35
  • @jiggunjer A function parameter `int*[N]` is a pointer to pointer, not an array of pointers. When I said they are the same, I really meant it. – juanchopanza Jul 07 '15 at 10:38
1
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int row = 5;
int column=20;
void dimensions (int* [20]);

int main (){

int *matriuprova[column];
for (int i= 0; i<column;i++){

    matriuprova[i] = new int [row];

    dimensions(matriuprova);// <-- here is the error:
        //main.cpp:14:31: error: cannot convert 'int**' to 'int*' 
        //for argument '1' to 'void dimensions(int*)'

}//end of for loop

return 0;
}//end of main

void dimensions (int *matriuprova [20]){
        //function code
         }

It's like the compiler said. It found the definition which was void dimensions(int [20]), which doesn't match your matrix. Correct the definition and it resolves the problem.

Thalia
  • 11,305
  • 15
  • 71
  • 169
kosmo16
  • 156
  • 10