954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

passing a matrix

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

drdolittle
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
//--------------------------------------

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;

//--------------------------------------
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 
//--------------------------------------

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?

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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

drdolittle
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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}};
grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 
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)
{
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
kux
Junior Poster
119 posts since Jan 2008
Reputation Points: 66
Solved Threads: 11
 

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[/QUOTE]

cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
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 functiondoes 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.

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You