C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 18th, 2003, 07:30 PM
bobbylalal bobbylalal is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 10 bobbylalal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old May 18th, 2003, 07:57 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
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];

Reply With Quote
  #3  
Old May 18th, 2003, 08:57 PM
bobbylalal bobbylalal is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 10 bobbylalal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #4  
Old May 18th, 2003, 09:03 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,508 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 16 m 59 sec
Reputation Power: 865

Reply With Quote
  #5  
Old May 19th, 2003, 02:45 AM
walter76 walter76 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 18 walter76 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Dynamic Arrays with undeclared size


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway