Mail Archives: djgpp/1999/07/07/01:22:07
leroy wrote:
> The following code doesn't do anything like what it should.
>
> for (index = 0; index != strlen(line); index++, line++)
> {
> tok = (char *)realloc(tok, index + 1);
>
> tok[index] = line[0];
>
> if (isbreakchar(tok[index]))
> {
> tok[index] = '\0';
> return tok;
> }
> }
>
> The code is supposed to take a line of input, and extract tokens from
> it, based on the value of isbreakchar().
>
> The problem is that the first letter never gets copied from line[0] to
> tok[0]. I've debugged everything as much as I could. Going through
> RHIDE, watching all variables concerned. It's as if that line just gets
> skipped.
Well, there is already a funciton in libc to do this "strtok". But
anyways, I don't see any obvious reason why this would skip the first
character, but I do see why it would skip the last half of the line. If
the 'line' has 2 characters, then after the first iteration, you will have
increased index AND line. That means that strlen(line) will return 1 and
index will equal 1, thus terminating your loop prematurly. Maybe you could
change your termination condition to "*line".
--
-Rolf Campbell (39)3-6318
- Raw text -