Make an array of char arrays

Jul 25, 2011 at 7:30am
So I am trying to make a char** that is named Final with x pieces and y chars in each piece...
char** Final = new char*[numb];
is my creation line but it will give me an error when i try to write a value to Final[anything greater than 1][anything] ex...
Final[2][0] = 't';
gives me an access error...
Last edited on Jul 25, 2011 at 7:31am
Jul 25, 2011 at 1:20pm
This char** Final = new char*[numb]; allocates an array of pointer (columns). You need to allocate space for pointing at (rows) as well:
1
2
3
4
5
char** Final = new char*[numb];
for(int i = 0; i < numb; ++i)
{
  Final[i] = new char[x];
}



Or better use a 1d array and calculate the offset yourself
http://www.cplusplus.com/articles/G8hv0pDG/
Jul 26, 2011 at 9:34pm
Thank you SO much for the 1D idea :)
Jul 27, 2011 at 1:21am
The problem with 1D idea is while it is "easier" for C/C++ developers, it may not look easy for the next person maintaining your code. To tackle this problem is to put comments on your code, especially on those offset calculation, the index is used for the first element then the next index is the next element for that first element etc etc.
Topic archived. No new replies allowed.