home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 241,437 of 243,242   
   Keith Thompson to Ros@invalid.invalid   
   Re: the index of one array that can not    
   23 Oct 25 15:30:48   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   Rosario19  writes:   
   > here the unsigned chars are 8 bit unsigned...   
   > here the index i it seems always definited from 0 to 255   
   > and so it is always definited a[i], but one can make errors the same   
   > in other places...   
   >   
   >   
   > #include    
   >   
   > main(void)   
   > {unsigned char a[258]="0123456789", i;   
   >  unsigned c;   
   >  for(c=i=0;c<500;++c,++i)   
   >      printf("%c",a[i]);   
   >  printf("\n");   
   >  return 0;   
   > }   
   >   
   > result   
   >   
   > C:\>index   
   > 0123456789   
   > 0123456789   
      
   Obviously "main(void)" should be "int main(void)".   
      
   Yes, if you use an unsigned char value as an array index, and the   
   array has at least UCHAR_MAX+1 (typically 256) elements, then the   
   indexing operation can't go outside the bounds of the array.   
      
   It's difficult to imagine this actually being useful.  There   
   might be some cases where you want an index to wrap around from   
   255 to 0, but this method doesn't allow for different lengths.   
   If you want an index to wrap around from 99 to 0, you have to do   
   it explicitly.  You might as well do it explicitly in this case   
   as well.  The resulting code would be a lot more legible and robust.   
      
   Also, you seem not to have noticed that you're printing the '\0'   
   characters that occupy most of your array.   
      
   --   
   Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com   
   void Void(void) { Void(); } /* The recursive call of the void */   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca