dynamically allocating multiple level char arrays...
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
--david@neonstar.netneonstar entertainment
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?
-----------------------------------------------All messages are of my own personal opinion and not meant to offend. But if they do - tough :)Neuro.
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:
and use double pointers to access it:
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
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
This this should work, your problem is that grid is a pointer to a pointer, and you created only 1 list
Can someone tell me how to put i between brackets?
like ? it thinks its something for inverse or whatever :p<br><br>and how do I become a zaelot or dedicated<br><br>Edited by - Jrz on June 16, 2000 10:40:14 AM
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 ? it thinks its something for inverse or whatever :p<br><br>and how do I become a zaelot or dedicated<br><br>Edited by - Jrz on June 16, 2000 10:40:14 AM
Check the FAQ''s on how to become dedicated and so on. Hint, its to do with the number of mails/replies you post.
-----------------------------------------------All messages are of my own personal opinion and not meant to offend. But if they do - tough :)Neuro.
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
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
--david@neonstar.netneonstar entertainment
I think it is rather ironic you ask about becomming dedicated and yet you show up as dedicated on my screen.
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. =)
There is no spoon.
wow, this post is old...
yeah, it works. it''s working beautifully right now in my map editor, neonMap.
don''t fake too hard, it might become real!
david
--david@neonstar.netneonstar entertainment
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement