|
|
#1 | |
|
Official pain in the ass
|
I'm new to C (been Java'ing for the past 3 years) and I need some help regarding strings and string arrays.
Basically, I want to read a bunch of names from a txt file and load them up into an array. How does one go about creating an array of strings in C? Mind you, I searched around through google but the answers I found I couldn't really comprehend. I would greatly appreciate any help... this is part of a project I have to hand in by Thursday. ![]()
__________________
My avatar: "An obscure and non sensical pattern made to get people to post what they think it is" - Vamp Dell XPS M1530 Core2Duo T5550 1.86Ghz | 3GB DDR2 | 8600M GT 256MB | 250GB Hdd | BenQ FP202W 20" LCD | MX1000 mouse | Inspire T5400 5.1 |
|
|
|
|
|
|
#2 | |
|
Registered User
Join Date: Jul 2005
Posts: 434
|
A "string" in C is implemented as a null-terminated array of characters.
So an array of strings in C would be an array of arrays, or an array of pointers to arrays. [0]---->[a][b][c][/0] [1]---->[1][2][3][4][/0] [2]---->[b][d][a][/0] Something like that. You're actually doing flat C, not C++? |
|
|
|
|
|
|
#3 |
|
xeroyphyte
Join Date: Mar 2004
Posts: 1,271
|
exactly like rhink stated.
"strings" are already an array terminated by null characters an array of strings: 0: [a][p][p][l][e][\0] 1: [o][r][a][n][g][e][\0] . . . etc so if you wanted to get to the 'n' in orange, the location would be [1][3], and that is printed as a char. i don't know if this helps, if your question is more advanced i could probably answer it if you ask the question. are you looking to create an array of strings? like a matrix array of char? looking at your question again, i'm not quite sure what you're asking.
__________________
HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3 XBL:Mr X3r0 -- PSN:Mr_X3r0 Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600* |
|
|
|
|
|
#4 | |
|
Official pain in the ass
|
I think I kinda figured it out. I know it's more or less a 2-dimensional array of chars, I just didn't know how to declare it.
As far as I can tell, its something like this: char *myArray[10]; char *str = "string!"; myArray[3] = str; That would leave "string!" in index 3 of myArray, wouldn't it?
__________________
My avatar: "An obscure and non sensical pattern made to get people to post what they think it is" - Vamp Dell XPS M1530 Core2Duo T5550 1.86Ghz | 3GB DDR2 | 8600M GT 256MB | 250GB Hdd | BenQ FP202W 20" LCD | MX1000 mouse | Inspire T5400 5.1 |
|
|
|
|
|
|
#5 | |
|
xeroyphyte
Join Date: Mar 2004
Posts: 1,271
|
Quote:
yes, myArray[3] would get the pointer value of str. but don't get confused, if you change what str points to down the line, it would also change the value of myArray[3]
__________________
HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3 XBL:Mr X3r0 -- PSN:Mr_X3r0 Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600* |
|
|
|
|
|
|
#6 | |
|
FOOL!
Join Date: Dec 2005
Location: Miami, Florida
Posts: 3,754
|
Quote:
char myArray[10]; char str = "string!"; myArray[3] = str; Wouldn't he get the same results or no?
__________________
Intel Q6600 QUAD|| Gigabyte P35-DS3L ||XFX 7900GTX|| G.Skill 6GB PC2-6400 || OCZ Agility 60GB SSD || WD 320GB + WD Green 1TB HDD || Samsung 20" LCD || CoolMax 650W PSU || 60gb PS3 Owner PSNid: Marvel_us |
|
|
|
|
|
|
#7 | |
|
xeroyphyte
Join Date: Mar 2004
Posts: 1,271
|
Quote:
second, if he wrote char myArray[10][X]; //where X is the max size of the string char str[X] = "string!"; //haven't worked with strings in a while might have to call a function to convert the string to the char array. don't remember. (#include string.h) then myArray[3] = str; then myArray[3] would contain the value of str, ie: "string!" and not a pointer to the pointer of the string.
__________________
HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3 XBL:Mr X3r0 -- PSN:Mr_X3r0 Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600* |
|
|
|
|
|
|
#8 |
|
FOOL!
Join Date: Dec 2005
Location: Miami, Florida
Posts: 3,754
|
Whoops wasn't really paying attention.
![]() Thanks for answering the question though....hopefully Tuork got it sorted out. I need to go start on some programming homework myself.
__________________
Intel Q6600 QUAD|| Gigabyte P35-DS3L ||XFX 7900GTX|| G.Skill 6GB PC2-6400 || OCZ Agility 60GB SSD || WD 320GB + WD Green 1TB HDD || Samsung 20" LCD || CoolMax 650W PSU || 60gb PS3 Owner PSNid: Marvel_us |
|
|
|
|
|
#9 |
|
Anisymbolic
Join Date: Aug 2004
Location: Wellington, New Zealand
Posts: 1,365
|
I notice someone mentioned "are you really using vanilla C rather than C++?" and I'm going to have to ask this as well. If so, is there a particular reason? Strings in vanilla C can get quite hairy, because they are just arrays of characters. If you declare something to be too small, you often won't be stopped from just overwriting memory that belongs to something else.
If you can use C++, I'd strongly recommend you do, because it'll only be fractionally slower, you can still use a straight procedural style of coding, and you get access to possibly the most useful thing ever: The Standard Template Library. Using STL you can do two things that will make your program more tidy. For one, you can use a vector to store your strings rather than an array, which will resize itself tidily and won't require you to know in advance how big it will be. (Yes, you could actually manually allocate memory using malloc, and then reallocate when you overflowed the memory you'd set aside, but that's painfully manual, and will open you up to all sorts of other possible problems if you're slightly careless.) Secondly, you can actually use standard strings, which allow for assignment more nicely than straight char*s, and, again, will resize nicely. Even if you don't like standard strings (which I'd understand, as of all the STL containers they're... not the most complete - they'd benefit hugely from an sprintf() function), you could still fill a vector with char*s. I do have one recommendation if you do go down the STL road though - fill the Vector with string pointers rather than strings. Now it's true that if you do that, you'll need to 'new' each string, and 'free' it when you destroy the vector, but strings, being STL containers will resize themselves quite a lot, and so will the vector itself. As you're probably working on a PC, where you have effectively infinite memory and fragmentation isn't such a concern, this probably doesn't matter so much, but it's good practice to try to avoid things constantly reallocating. Edit: Also, I believe if you did want to do it using a straight array of pointers to strings, you could just go char* myarray[10]; myarray[x] = (char*)malloc( sizeof(char) * numberOfCharacters ); strcpy( myarray[x], "Hello!" ); Edit 2: Oops - didn't see that you'd pretty much already written that. Note: You will want to use strcpy/sprintf and such things for string assignments.
__________________
Dr Possible: Core 2 Duo E6400 on Gigabyte GA-965P-DS4. Galaxy GeForce 7600GT. 2GB Corsair XMS 2 DDR2-6400 RAM (CL5). ATi Theatre 550 Pro. Windows XP MCE. All stored in Piano black Antec Sonata II, with a broken door. Mobile: ASUS M2400N, Pentium M 1.5 GHz. 512 MB DDR RAM. Intel EXTREME graphics. Windows XP SP 2 / Ubuntu 5.10. Ridiculous DOES not have an 'e' in it. It comes from "ridicule" and has less than nothing to do with the colour red. Last edited by Subtestube; 02-07-07 at 05:05 PM. |
|
|
|
|
|
#10 |
|
Official pain in the ass
|
Thank you all for the added input.
My deadline was pushed so I guess I'll be working on this over the weekend. As for the whole C/C++ business, I'll ask my teacher on fruday, but as far as I know, it's supposed to be in C. Yes, C++ would have the advantages you mentioned, but orders are orders :P. Besides, it's nothing too complex... just a very *VERY* simple initial stage of a P2P program we will build.
__________________
My avatar: "An obscure and non sensical pattern made to get people to post what they think it is" - Vamp Dell XPS M1530 Core2Duo T5550 1.86Ghz | 3GB DDR2 | 8600M GT 256MB | 250GB Hdd | BenQ FP202W 20" LCD | MX1000 mouse | Inspire T5400 5.1 |
|
|
|
|
|
#11 |
|
Official pain in the ass
|
Uuuuupdate:
Well, I figured out how to do the string thingers (I'm using a structure in order to assure scalability). The real question now is... how to maximize memory efficiency. Right now I am using character arrays of a set size (200) for two variables, name and path. How can I use pointer to manage memory in a more elegant manner?
__________________
My avatar: "An obscure and non sensical pattern made to get people to post what they think it is" - Vamp Dell XPS M1530 Core2Duo T5550 1.86Ghz | 3GB DDR2 | 8600M GT 256MB | 250GB Hdd | BenQ FP202W 20" LCD | MX1000 mouse | Inspire T5400 5.1 |
|
|
|
|
|
#12 |
|
xeroyphyte
Join Date: Mar 2004
Posts: 1,271
|
basically you need to look at testtube's response
you need to memory allocate the size of the string you attempting to store, and be sure to release that memory after you are finished using it.
__________________
HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3 XBL:Mr X3r0 -- PSN:Mr_X3r0 Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600* |
|
|
|
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pure Storage Launches Next Generation All-Flash Storage Array | News | Latest Tech And Game Headlines | 0 | 06-11-12 09:40 PM |
| GL_EXT_texture_compression_s3tc support for Quadro4 700 XGL | marvin24 | NVIDIA Linux | 3 | 08-20-03 09:00 AM |
| Major WineX prob... I think it has to do with the vidcard... | Linewbie | NVIDIA Linux | 20 | 10-09-02 09:58 PM |
| TNT2 - DXTC/ S3TC extensions .. | DarkYY | NVIDIA Linux | 19 | 10-01-02 12:31 PM |
| Xtacy t14600, Redhat 7.3.. X but no logo. | Rob C. | NVIDIA Linux | 8 | 08-12-02 09:28 AM |