delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/07/01:22:07

From: "Campbell, Rolf [SKY:1U32:EXCH]" <cp1v45 AT americasm01 DOT nt DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Hmmm, weird problem...
Date: Tue, 06 Jul 1999 09:54:59 -0400
Organization: Nortel Networks
Lines: 38
Message-ID: <37820AB2.9E89E497@americasm01.nt.com>
References: <377F7C68 DOT B7AA796B AT primus DOT com DOT au>
NNTP-Posting-Host: bmerhc00.ca.nortel.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.6 [en] (X11; I; HP-UX B.10.20 9000/712)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019