gh wrote:
I have a string variable which contains n number of comma delimited
elements and I would like to store each element into an array but I
could not figure how to do it.
for example, 1stitem,2nditem,3rditem,4thitem, ..., nthitem
What would be the best way of storing the above string into an array
like
tempary[0]="1stitem"
tempary[1]="2nditem"
tempary[2]="3rditem"
tempary[3]="4thitem"
...
...
tempary[n-1]="nthitem"
You don't say if these are static values, or you want a general purpose
set of mutable strings.
I typically build a array of pointers to some char arrays (or char
pointers) of sufficient size (determined at runtime or compile time --
your choice depending on your application).
For example, I will often declare a char **, allocated space for the
number of char * items I want to store in it (adding room for one if I
want the notion of the last one in the "list" being null as a marker)
and then allocate room for each string as needed, associating them with
the container pointer "array".
I'm hand-waving here, but think of what "**argv" or "*argv[]" means.