![]() |
|
#1
|
|||
|
|||
Declaring an array of strings dynamically (C Language)Hello everybody!
I have restarted to program in C after long years without working with it, and I have a doubt. I need to declare an array of strings, but I don't know how muchs strings will have to store in this array. I have declared my array like this, because I want that every position of the array can handle a string of 10 chars. CPP / C++ / C Code:
Later, when I know how much array members I need, I tried to allocate memory to handle them: CPP / C++ / C Code:
Can you help me? |
|
#2
|
|||
|
|||
|
Quote:
Quote:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
|
Quote:
Quote:
|
|
#4
|
|||
|
|||
|
Quote:
CPP / C++ / C Code:
after above decleration the situation is something like following: Code:
Code:
Hope that clarified things |
|
#5
|
|||
|
|||
|
Yes, it clarified things. And now I realize that this is not what I need. I need a X lenght array, I don't know X until some point of the program. So what I really need is an array that has one string in every position:
arrayIndices[1] = "string1" arrayIndices[2] = "string2" arrayIndices[3] = "string3" . . . arrayIndices[X] = "stringX". Can I achieve this, or it is impossible? I mean, can I indicate the lenght of my array programatically? Maybe I should use this declaration: char **arrayIndices indicating that this is an array of char pointers, or something like that. I hope I'm explaining my problem well....if I'm not, let me know and I will retry |
|
#6
|
|||
|
|||
|
I'm, more or less, in the right path
I've found a solution in this thread: Pointers in C (Part I). All I have to do is to declare my array as I mentioned CPP / C++ / C Code:
And next, when I have the number of files, allocate the necessary space for every string: CPP / C++ / C Code:
Where numIndice is the string I want to store, and indexArray is the position index of the array. Thanks for your help |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sort an array of structures | sassy99 | C Programming Language | 10 | 15-Feb-2007 08:46 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 22:26 |
| array of pointers to strings | mirizar | C++ Forum | 5 | 21-Jan-2005 11:24 |
| Creating N string | gwk | C Programming Language | 3 | 21-Jul-2004 00:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The