Re: from array of ints to string and vice versa
From: Willem (willem_at_stack.nl)
Date: 10/19/04
- Next message: Arthur J. O'Dwyer: "Re: XPM image file format"
- Previous message: Ben Pfaff: "Re: D Programming (Class Protection)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 19 Oct 2004 16:39:38 +0000 (UTC)
Martijn wrote:
) Hi,
)
) I have the following issue: A user is allowed a enter a sequence of
) numbers, separated by comma's. I want to convert this string to an array,
) using the C language, taking "abundant" whitespace into account. I
) currently do this by running over the string in two passes, one to validate
) the string - trap on letters and invalid things such as "8,,9" and "8,9
) 8,9" - and to calculate the required size of the integer array, and another
) to extract the actual numbers.
)
) It works like a charm (code on request), but I was wondering if there is a
) more straightforward way to accomplish this? For the reversed process I do
) something similar - one pass to calculate the size of the string, and the
) other to generate it, separating them with ", ".
Well, the two-pass method to first calculate the size, and then fill the
array, is IMO the most straightforward way of doing things, in C that is.
Otherwise you'd need some way to dynamically grow the thing.
One way that may or may not be more elegant is to have a function that
takes a buffer and returns a size, which you first call with a NULL pointer
to have it calculate the size, then you allocate the buffer, and finally
you call the same code again with the buffer allocated to the required
size. Then, insize the code, you just conditional(*) on the pointer to see
if it's the dry run, or the actual run.
*) You can verb any noun. :-)
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
- Next message: Arthur J. O'Dwyer: "Re: XPM image file format"
- Previous message: Ben Pfaff: "Re: D Programming (Class Protection)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|