Can somebody explain whats wrong in the following code ? I am trying to
pass a matrix and the called function is a double pointer.

#include <iostream>

void func(int **);

int main(int argc,char *argv[])
{
	int A[2][2]={1,2,3,4};
	func(A);
}
void func(int **A)
{
}

Best,
Pradeep

My first thought is because int** is not the same as int[][], though the syntactic use of each is quite similar.

My first thought is because int** is not the same as int[][], though the syntactic use of each is quite similar.

You're quite right. The int** is a pointer that would be allocated an array of pointers, each of which point to an array of ints. int[][] is a contiguous block of memory, which can only be accessed correctly when the column dimension of the argument array and the column size in the function's formal parameter are the same.

//--------------------------------------

int A[2][2]={1,2,3,4}; //--overflow
func(A);

//--------------------------------------

int A[1][4];
A[0][0] = 1;
A[0][1] = 2;
A[0][3] = 3;
A[0][4] = 4;

//--------------------------------------
//--------------------------------------

int A[2][2]={1,2,3,4}; //--overflow
func(A);

//--------------------------------------

int A[1][4];
A[0][0] = 1;
A[0][1] = 2;
A[0][3] = 3;
A[0][4] = 4;

//--------------------------------------

What? Where is overflow?

okay, but nobody has suggested how to correct the error. I think its the function declaration which has an error.

Try

void func(int (*)[2]);   // declaration

void func(int (*x)[2])
{
    // whatever
}

In this exmaple, func accepts an argument of type "pointer to array of two ints".

The initialisation of A in your example should be something like;

int A[2][2]={{1,2},{3,4}};

okay, but nobody has suggested how to correct the error. I think its the function declaration which has an error.

The solution is to decide how you want to allocate the matrix, and use function parameter that is formatted the same.

//either
void func(int [][2]);

int main(int argc,char *argv[])
{
	int A[2][2]={1,2,3,4};

	func(A);
}
void func(int A[][2])
{
}

// --- OR ---

void func(int **);

int main(int argc,char *argv[])
{
	int **A;
        A = new int *[2];
        A[0] = new int[2];
        A[1] = new int[2];
        //now go store data in the matrix

	func(A);
}
void func(int **A)
{
}

Use reinterpret_cast or c style cast to casting..
But the matrix will become 1 dimension matrix..

#include <iostream>

void func(int **);

int main(int argc,char *argv[])
{
    int A[2][2]={1,2,3,4};
//--reinterpret_cast
      func(reinterpret_cast<int**>(A));
//--c style cast
      func((int**)A);   //--func(A);
}
void func(int **A)
{
}

Best,
Pradeep

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I thought of the typecasting solution - it doesn't work. The function is still expecting a pointer to array of pointers, which the array in main is not.

Pradeep's code will compile, but access to the array in func( ) will not work.

Use reinterpret_cast or c style cast to casting..

That's using a sledgehammer to crack a nut. Problem is, it doesn't work because it destroys the nut.

A cast forces the code to compile, but then the result is a program that won't work as expected. Effectively it forces the compiler to pass an argument to a function when that function expects something different. If that function does anything with that argument, the result is undefined behaviour.

The better approaches are;

1) Change the argument types the function accepts, so it matches what you'll attempt to pass. I gave an example of that in my previous post.

2) Construct data in a form that is consistent with what the function expects. vmanes second example (after the // --- OR --- comment) is an example of that.

This article has been dead for over six months. Start a new discussion instead.