1

I am trying to pass a 2D-array to a function using double pointer,however the compiler is giving the following error

[Error] cannot convert 'int (*)[3]' to 'int**' for argument '1' to 'void print1(int**, int, int)

Here is the code:

#include<iostream>
using namespace std;
void print1(int **arr,int r,int c);

int main()
{
    int a[2][3]={{10,20,30},{40,50,60}};

    int r=2;
    int c=3;
    print1(a,r,c);
}
void print1(int **arr,int r,int c){
    int i,j;
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            cout<<arr[i*r+j]<<"\t";
        }
    }
}

I want to emulate two dimensional array using double pointer.

user3262269
  • 113
  • 3
  • 13

1 Answers1

2

A multidimensional array is not an array of pointers. int** means a pointer to a pointer where as int (*)[3] means a pointer to an array of 3 ints. You should change your print1 definition to this:

void print1(int (*arr)[3],int r,int c)

You're also using arr wrong inside of the function. It looks like you're trying to use the pointer as an integer pointing to the beginning of the entire memory region. You should either change it to

cout<<arr[i][j]<<"\t";

or change the definition of print1 to

void print1(int *arr,int r,int c)

Kurt Stutsman
  • 3,785
  • 15
  • 23