www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/05/22:16:49

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: For loop problem
Date: Sun, 05 Apr 1998 22:04:58 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 33
Message-ID: <3528384A.7BAD@cs.com>
References: <3527FEA6 DOT 54FA AT vegas DOT infi DOT net>
NNTP-Posting-Host: ppp225.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Simz wrote:
> 
> When ever I try to use a for loop in DJGPP with a char (or any type for
> that matter), the loop never stops when I try to loop to the max value
> that type can hold. For example
> 
> unsigned char index = 0;
> unsigned char array[256];
> for (index = 0; index < 256; index++)
>         array[index] = index;

Use a larger type.  What you describe will not work with any C compiler,
much less djgpp.  And since array indexes are ints, there's no reason
not to use an int in the loop instead of a char.  Putting an int into a
char array is no problem; it's an implicit typecast.  I should also
mention that initializing index and then setting it in the for loop is
redundant.

    unsigned char array[256];
    int index;
    for ( index = 0; index < 256; index++ )
        array[index] = index;   /* or (char) index if it makes you 
                                   feel better */

hth

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Animals can be driven crazy by pla- |
|       aka Fighteer I       | cing too many in too small a pen.    |
|   mailto:fighteer AT cs DOT com   | Homo sapiens is the only animal that |
| http://www.cs.com/fighteer | voluntarily does this to himself."   |
---------------------------------------------------------------------

- Raw text -


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