oh the pain, the pain of c
I need to translate this simple perl line into C:
@array = split(/:/, $string); I know I'm going to need to use strtok, but my problem is that I don't know how big array has to be until I tokenize string, so I can't declare array because I don't know what size to make it. Do I have to parse it twice to find the number of tokens first and to store them second? This seems really inelegant, and I'm sure there's a better way. Can anybody help? Thanks, btb |
Well your options would be to:
* make an array which is always large enough * count how many you would need first (probably would need to parse twice) * use an array which can dynamically resize itself (if you are using c you will have to build your own implementation of that) |
I'm sure you can get creative with the malloc() and realloc() functions. Check them out. I say you break it down like this:
1. malloc() out some memory enough to hold a good amount of data. 2. Create a function that walks through the string and counts the number of words found (excluding the colon-delimiters). 3. Then take that and check if you have to allocate more space. 4. If you need more space, do a realloc() and proceed with your tokenizing. Good luck ;) |
All times are GMT -5. The time now is 10:07 AM. |