TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Jasen Betts
from: Robin Sheppard
date: 1999-01-14 13:36:00
subject: Array Problem

RS>     char array[20] = "hi!";
 
 RS>   will assign NUL to array[3], correct?  And since C doesn't check
 RS>   array subscripts, that would happen regardless of how many elements
 RS>   the array was declared to have.

 JB> no,  the compiled code has no way to know the size of the array, but
 JB> the compiler does know during initialisation, the compiler
 JB> takes it into account during initialisation...

 JB> char array[20]="hi";

 JB> will compile differently to

 JB> char array[20]; strcpy(array,"Hi!");

   Yeah, it will, because the first allocates 20 bytes, and sets the 
   first three bytes to 'h', 'i', and 0, respectively, whereas the 
   second just allocates 20 bytes, and waits until strcpy() changes 
   them.
   
   My point wasn't this; it was rather that the NUL is tagged onto the 
   ends of strings if you initialized them when you declared them.  
   For example, 
   
     char no_no[24] = "we're gonna have trouble";
   
   will still attempt to store this string (24 bytes plus the NUL), 
   even though there isn't room.  You're right, a good compiler may 
   issue a warning here, but that's about it.  If you run this code, 
   you're just asking for trouble, although in reality, you probably 
   won't crash your system.  Rather, you'll probably just muck up 
   whatever data immediately follows the array no_no.

... Death is just another expression of obsolescence.
___ Blue Wave/DOS v2.30 [NR]

--- Maximus 2.01wb
* Origin: Basic'ly Computers: Mooo-ing Right Along. (1:153/9)
SEEN-BY: 396/1 632/0 371 633/260 262 267 270 371 634/397 635/506 728 639/252
SEEN-BY: 670/218
@PATH: 153/104 2 716 7715 140/1 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.