| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Array Problem |
JB> char array[20]="hi"; JB> will compile differently to JB> char array[20]; strcpy(array,"Hi!"); RS> Yeah, it will, because the first allocates 20 bytes, and sets the RS> first three bytes to 'h', 'i', and 0, respectively, whereas the RS> second just allocates 20 bytes, and waits until strcpy() changes RS> them. More than that, the first one will allocate 20 bytes, set the first to 'h', the second to 'i', and the rest to nul. The second one will leave the entire block of 20 characters alone, and then proceed to change the first three, without changing the last seventeen. RS> My point wasn't this; it was rather that the NUL is tagged onto the RS> ends of strings if you initialized them when you declared them. RS> For example, This is not entirely true. char array[2] = "hi"; This you think should take three bytes - but ANSI C allows this particular construct to take the two you asked for. You're asking for trouble if you try to use printf, strcpy, or any other "string"-like function, on it. However, accessing array[0] and array[1] is perfectly fine. RS> even though there isn't room. You're right, a good compiler may RS> issue a warning here, but that's about it. If you run this code, Nope - a good compiler should say nothing. ---* Origin: Tanktalus' Tower BBS (1:250/102) 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: 250/102 201 99 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™.