The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Dynamic Arrays with undeclared size
Discuss Dynamic Arrays with undeclared size in the C Programming forum on Dev Shed. Dynamic Arrays with undeclared size C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 18th, 2003, 07:30 PM
|
|
Registered User
|
|
Join Date: May 2003
Posts: 10
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Dynamic Arrays with undeclared size
from a friend:
i wanna declare a string array, however, i dunno the size of the array before the call the main, sth like this:
int num;
char *names[]; // this line looks wrong, but i dunno how else to put it
....
int main() {
....
num = some_number // num gets assigned;
names = new char[num][256];
....
}
256 can be the max length of the strings in the array, if allocating that memory at the beginning is of any use
i didn't tho, b/c i'm thinking, array of strings is actually a pointer to an array of chars, so it'd look like char[][256], but the compiler's gonna complain about that...
i've tried various combinations of char[][], *char[], etc etc...
these generate conversion errors...
the whole problem stems from the fact that i must dynamically assign the size (i.e. dunno the size of the array before getting to main)
thx!! i also dont mind if someone'd tell me how to assign strings into the array after successfully declaring the array also, just in case.
|

May 18th, 2003, 07:57 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
you were close. you just need to remove the last brackets in this line: names = new char[num][256];
so it looks like this: names = new char[num];
that will create an array of maxSize num.
edit: the new function returns a pointer to the allocated memory. so names must be a pointer to a character:
char *names = new char[num];
|

May 18th, 2003, 08:57 PM
|
|
Registered User
|
|
Join Date: May 2003
Posts: 10
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Ah...i c...I'll test it out later tonight....
thanks...
p.s. additional replys or rebuttals will be good too...it's nice to learn different solutions and other ppl's views...
thanks!
|

May 18th, 2003, 09:03 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|

May 19th, 2003, 02:45 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Remember,
Code:
char **names = (char**) new char*[num];
only declares a array of pointer to string objects. You have to alloc memory for every string, too.
Code:
for(int i=0; i<num; i++) {
names[i] = (char*) new char[256];
}
Do not forget to free the memory you've assigned.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|