| | |
passing a matrix
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Can somebody explain whats wrong in the following code ? I am trying to
pass a matrix and the called function is a double pointer.
Best,
Pradeep
pass a matrix and the called function is a double pointer.
C++ Syntax (Toggle Plain Text)
#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
Last edited by drdolittle; Nov 6th, 2008 at 8:56 pm.
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
c++ Syntax (Toggle Plain Text)
//-------------------------------------- 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; //--------------------------------------
Last edited by cikara21; Nov 7th, 2008 at 3:11 am.
•
•
•
•
c++ Syntax (Toggle Plain Text)
//-------------------------------------- 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; //--------------------------------------
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
Try
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;
C++ Syntax (Toggle Plain Text)
void func(int (*)[2]); // declaration void func(int (*x)[2]) { // whatever }
The initialisation of A in your example should be something like;
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
//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) { }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Use reinterpret_cast or c style cast to casting..
But the matrix will become 1 dimension matrix..
Best,
Pradeep[/QUOTE]
But the matrix will become 1 dimension matrix..
c++ Syntax (Toggle Plain Text)
#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]
![]() |
Similar Threads
- How to pass reference of a 2-D matrix to a function in C (C)
- Passing a matrix from main function to user defined function. (C++)
- Playing cards (C++)
Other Threads in the C++ Forum
- Previous Thread: Tough time solving this problem. Month names to store in array and manual entry?
- Next Thread: Cursor pointer to a pointer to a pointer
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






