| | |
Two dimensional char array 2d char array allocation problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hi,
I am facing a problem with allocation of two dimensional char array. The problem statement requires me to get the number of rows and columns of the array from a file. I have saved the rows in "rows" and columns in "columns", both of type int.
Here is how i initialized the array.
char *array;
array = new char [rows][columns];
The error I get is "non constant expression as array bound".
I know what constant expression means. But I am unable to figure out the way to make a 2d dynamic array by getting its size from the file.
Please help me with this thing.
I will appreciate this if you tell me how to make a dynamic 2d char array by getting the size of it from a file.
Thx.
Samran.
I am facing a problem with allocation of two dimensional char array. The problem statement requires me to get the number of rows and columns of the array from a file. I have saved the rows in "rows" and columns in "columns", both of type int.
Here is how i initialized the array.
char *array;
array = new char [rows][columns];
The error I get is "non constant expression as array bound".
I know what constant expression means. But I am unable to figure out the way to make a 2d dynamic array by getting its size from the file.
Please help me with this thing.
I will appreciate this if you tell me how to make a dynamic 2d char array by getting the size of it from a file.
Thx.
Samran.
•
•
Join Date: Feb 2008
Posts: 638
Reputation:
Solved Threads: 46
I really never use arrays, I go for the vector of vectors
If you want to use an actually array you have to use "new", because as it's telling you, you aren't allowed to determine the size of an array declared like you were doing it at runtime.
C++ Syntax (Toggle Plain Text)
vector<vector<char> > array(columns); for(unsigned int i = 0; i < columns; i++) { vector<char> RowVector(rows); // ... fill the row vector ... array.push_back(RowVector); }
If you want to use an actually array you have to use "new", because as it's telling you, you aren't allowed to determine the size of an array declared like you were doing it at runtime.
Last edited by daviddoria; Mar 7th, 2009 at 1:16 pm.
>>char *array;
That is not a 2-dimensional array.
Here is one way to initialize it
That is not a 2-dimensional array.
char **array is how to declare it, using two stars not just one.Here is one way to initialize it
C++ Syntax (Toggle Plain Text)
char **array = 0; array = new char*[rows]; for(int i = 0; i < rows; i++) array[i] = new char[columns];
Last edited by Ancient Dragon; Mar 7th, 2009 at 1:28 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- allocation issue (C++)
- Pointers (archived tutorial) (C++)
- Array problems (C++)
- Pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: Mutually defined structs
- Next Thread: Constant member function
Views: 1804 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






