LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Arrays in C (https://www.linuxquestions.org/questions/programming-9/arrays-in-c-573405/)

rubadub 07-30-2007 10:44 AM

Arrays in C
 
Hi,
I'm after an efficient way of putting the results of strtok into an array and return it from a function. I don't want to use linked-lists, stacks, etc, just a plain array.

The way i'm doing it at the moment reallocates two sets of memory on every pass of strtok. The other way i've done it is to declare lot's (max) of memory and then to copy it into a resized array at the end.

Is there a simpler way?

p.s. Is there a function which tells you how many elements strtok will output (like in java)?

Mara 07-30-2007 02:26 PM

I'd allocate the memory of length equal to the string length plus an index table (of resonable length, max strlen/strlen (delim) ) and put the subsequent results as new string, saving start of each of them in the index table.

You can use a loop of strstr() to find out how many elements will be returned.


All times are GMT -5. The time now is 07:06 AM.