Two dimensional char array 2d char array allocation problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 21
Reputation: Samran is an unknown quantity at this point 
Solved Threads: 0
Samran's Avatar
Samran Samran is offline Offline
Newbie Poster

Two dimensional char array 2d char array allocation problem

 
0
  #1
Mar 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Two dimensional char array 2d char array allocation problem

 
0
  #2
Mar 7th, 2009
I really never use arrays, I go for the vector of vectors

  1. vector<vector<char> > array(columns);
  2. for(unsigned int i = 0; i < columns; i++)
  3. {
  4. vector<char> RowVector(rows);
  5. // ... fill the row vector ...
  6. array.push_back(RowVector);
  7. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Two dimensional char array 2d char array allocation problem

 
0
  #3
Mar 7th, 2009
>>char *array;
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
  1. char **array = 0;
  2. array = new char*[rows];
  3. for(int i = 0; i < rows; i++)
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 21
Reputation: Samran is an unknown quantity at this point 
Solved Threads: 0
Samran's Avatar
Samran Samran is offline Offline
Newbie Poster

Re: Two dimensional char array 2d char array allocation problem

 
0
  #4
Mar 8th, 2009
Thanks Ancient Dragon.
Samran Asghar.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1804 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC