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;
//--------------------------------------
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.
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
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