| | |
Dynamic 2d array not passing?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 30
Reputation:
Solved Threads: 1
Hey, im trying to print what is stored in my 2d dynmic array from a file in my print function. However whenever i try to access information in the array, it crashes the program. maybe the array is not being passed back some how, im not sure, any information would be helpful.
In the function getData(), i can output the array.
Attached is all the program files with a test file "data.txt"
Thank you in advance
In the function getData(), i can output the array.
Attached is all the program files with a test file "data.txt"
Thank you in advance
C++ Syntax (Toggle Plain Text)
void getData(int**,int&,int&); void print(int**,int,int); int main() { int **array; int row, column; getData(array,row,column); print(array,row,column); system("PAUSE"); return 0; } void print(int **array, int row, int column) { for(int y=0; y < row; y++) { cout << endl; for(int x=0; x < column; x++) { cout << array[y][x] << " "; } } cout << endl; } void getData(int **array, int &row,int &column) { string fileName; ifstream data; cout << "Enter File Name\n"; getline(cin, fileName); data.open(fileName.c_str()); if (!data.is_open()) cout << "Error opening file\n"; else { data >> row >> column; array = new int*[column]; //Allocates memory for the array for (int q = 0; q < column; ++q) array[q] = new int[row]; for(int y=0; y < column; y++) { for(int x=0; x < row; x++) { data >> array[y][x]; } } } data.close(); }
Your getData( ) function is being passed the int ** BY VALUE. It can allocate memory to that pointer variable, but that allocation only exists within the scope of the function. The int ** in main( ) is still not allocated any memory.
So, to make life interesting, you must pass the int** by reference, like:
Also, in getData( ), you are using the row and column variables backwards. Do the allocations and reading loops with row as the outer loop, column as the inner loop, as you do in your display function.
So, to make life interesting, you must pass the int** by reference, like:
C++ Syntax (Toggle Plain Text)
void getData( int** & ,int&,int&);
Also, in getData( ), you are using the row and column variables backwards. Do the allocations and reading loops with row as the outer loop, column as the inner loop, as you do in your display function.
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.
![]() |
Similar Threads
- get length of a dynamic array (C++)
- Passing 2D Array of Pointers into a function (C++)
- Passing pointer to matrix to caller? (C++)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- passing the recordset data into an Array (ASP)
- Passing a filestream, how? (C++)
- Problem converting Selection Sort to Class (C++)
- Reading CSV in VC++ (C++)
- How to be Crash Free (C++)
- Array limit (C)
Other Threads in the C++ Forum
- Previous Thread: simple help needed
- Next Thread: ISBN Help
Views: 448 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






