Archived

This topic is now archived and is closed to further replies.

dynamically allocating multiple level char arrays...

This topic is 6388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

yep, it''s a long post title... what i''m trying to do is load a file that has a table of information bits and i need to be able to dynamically allocate a char structure like BitTable[][] at run time so that i can have any different number of bits in the file. i''ve tried having a **BitTable and going like BitTable = new char[BitNum] then going in my for loop and saying BitTable[c] = new char[20] but it doesn''t work. can anyone help? david

Share this post


Link to post
Share on other sites
How about initially allocating the memory with malloc or new. This would be of a set block size. Then if you read up until the end of this size use ''realloc'' to double its size or add enough memory for another block?

Share this post


Link to post
Share on other sites
I don't think you can initialize double pointers, but they can be used to access multiple arrays.

The best way would be to create an array of char pointers:


char* names(10);


and use double pointers to access it:


char* names(2);
char** ptr;

names(0) = new char[20];
names(0) = new char[20];

strcpy(names[0], "Michael");
strcpy(names[1], "Paul");

ptr = names;
..
..
// This will show the full name "Michael"
printf("%s", *(ptr));
..
..
// This will show the first letter of the second name 'P'
printf("%c", *(*(ptr + 1)));
..
..
// And this will show the third letter of the second name 'u'
printf("%c", *((*(ptr + 1)) + 2));


If anyone knows of a way to allocate memory to a double pointer I would be interested to know.

Michael

Edited by - gavcc on June 16, 2000 8:55:43 AM

Share this post


Link to post
Share on other sites
This this should work, your problem is that grid is a pointer to a pointer, and you created only 1 list
            
char **grid;
int num=12;
grid = new char*[num];

for (int i=0; i<num; i++)
grid[ i ] = new char[20];



Can someone tell me how to put i between brackets?
like [ i ]? it thinks its something for inverse or whatever :p

and how do I become a zaelot or dedicated

Edited by - Jrz on June 16, 2000 10:40:14 AM

Share this post


Link to post
Share on other sites
hey, thanks guys, great info.
i''ll put that code in there when i get home

i didn''t have the * in BitTable = new char[BitNum];
i''l bet that''s the problem.

thanks again,
dave

Share this post


Link to post
Share on other sites
Im still trying to get my dedicated so Ill reply... Ive been having this exact problem so I cant wait til I get off of work so I can try out your suggestions... Im tempted to fake a headache so I can bone out early. =)

Share this post


Link to post
Share on other sites