Re: parsing string into an array
- From: Guillaume <"grsNOSPAM at NOTTHATmail dot com">
- Date: Sun, 10 Apr 2005 17:50:24 +0200
John Smith wrote:
> To store the output to a string array, I created a two-dimensional
> array and copied the output to the array:
>
> char output[5][55];
> (snip)
> This doesn't work. What's in the output array is nothing like the
> output (on screen). How should it be done?
Are you aware that this will fail if:
* there are more than 5 "tokens"
* one or more of the tokens is (are) longer than 54 characters
That said, there are a couple more things to know:
* strtok() alters the string it works on. So you can't use
it to parse the same string twice unless you have made a copy
of this string before. This may be what happens to you if
your program tries both of your parsing attempts one after
the other on the same string.
* strtok() is one of those "evilish" C std library functions,
because it maintains an internal structure and thus cannot be
used in nested calls. So you must use it with a lot of care,
and it really is safe only in the simplest programs (unless
you really know what you're doing).
Not worth it in my opinion, since it's so easy to implement
"by hand" in a safe manner.
As a side note, I will add that outside of textbooks, multidimensional
arrays in C are not particularly useful. Their syntax is often
misleading (in my opinion) and they can serve only very limited
purpose. I tend to prefer arrays of arrays, which are easier to handle
and more flexible (in my opinion).
.
- Follow-Ups:
- Re: parsing string into an array
- From: Keith Thompson
- Re: parsing string into an array
- References:
- parsing string into an array
- From: John Smith
- parsing string into an array
- Prev by Date: Re: parsing string into an array
- Next by Date: Re: parsing string into an array
- Previous by thread: Re: parsing string into an array
- Next by thread: Re: parsing string into an array
- Index(es):
Relevant Pages
|