delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/10/17:15:28

From: ams AT ludd DOT luth DOT se (Martin Str|mberg)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: it's amazing (at least for me)
Date: 10 Jul 1999 11:59:50 GMT
Organization: University of Lulea, Sweden
Lines: 58
Message-ID: <7m7cjm$f5g$1@news.luth.se>
References: <378618ca DOT 3495674 AT news DOT telepac DOT pt>
NNTP-Posting-Host: queeg.ludd.luth.se
X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

aperes (ajps AT mail DOT telepac DOT pt) wrote:
: In this little C program if i change the type of variable k (in setup
: function) to byte the program don't work. Can anyone explain me why?
: Thanks
: 
: APeres.
: 
: ------------------------------------------------------------------------------------
: typedef  unsigned char  byte;
: typedef  unsigned char  pallette[256][3];
: 
: void setup(pallette);
: 
: int main(void)
: {
:    int i;
:    pallette pall;
: 
:    setup(pall);
: 
:    for(i=0; i < 256; i++)
:       printf("%d ------- %d\n", i, pall[i][0]);
: 
:    return 1;
: 
: }
: 
: void setup(pallette pall)
: {
:    int k;
: 
:       for(k=0; k < 256; k++)
:       pall[k][0] = pall[k][1] = pall[k][2] = i;
: }

Alas it's not compilable. In setup() i is undeclared.

Let's I change that i to k, which is what I think you meant.

Now as you say it works with "int k;"

But it also work in the case of "byte k;". The problem is you have a
missguided idea of what "it works" means in this case. If k is an
unsigned char then k's maximum value is 255, hence the for loop's
continuation expression is always true, which leads to endless
looping.

If you had run your program in a debugger you'd seen this (which I had
to do to realise what was happening).

Another little problem (which is unrelated) is that you haven't
#included stdio.h. This you would have found if you were using the
gcc flag "-Wall".


Skunk Anansie, Paranoid & Sunburnt,

							MartinS

- Raw text -


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