TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Robin Sheppard
from: Darin McBride
date: 1999-01-17 15:10:02
subject: Array Problem

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

 RS>    the string when strcpy() is called.  As for the first, I wasn't 
 RS>    aware that the remainder of the array was padded with NULs.  Is 
 RS>    this just how some compilers do it, or is it mandated by the 
 RS>    standard?  I can't see any reason for this being necessary, since a 
 RS>    second just allocates 20 bytes, and waits until strcpy() changes 
 RS>    them.

Ah - you think of char[]'s as "strings".  The language does not -
it sees char[] as "array of char," or, "array of signed
byte."  (Here 'byte' is determined to be the smallest unit of memory
that can hold the character set, or, a char.)  The fact that there is a
convention of using nul-terminated blocks of characters as a
"string" is immaterial.

The language, however, recognizing this popular convention, allows strings
in quotes to be equivalent to an array of the same characters, with a nul
terminator if there is room (*).  For example:

"hi" == {'h', 'i', '\0'}

(*) this only applies in an assignment.  There are more ways to use this
convention, primarily, as a pointer to that data structure in memory.

Therefore, rather than writing the following code:

char hello_world[LEN] = { 
    'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\0'
};

we can write:

char hello_world[LEN] = "Hello world!";

Now, for some interesting bits... if we change the macro "LEN"
above, we can get some interesting quirks.

1. LEN < 12
        Compilers should complain about excess elements in the array.
2. LEN = 12
        The former, having explicitly thirteen characters, has an excess
element in the array.  The latter, having the nul only being implicit, does
not have any excess character.
3. LEN = 13
        Both are fine, and have exactly the same data in them.
4. LEN > 13
        As with LEN = 13, both have the same data in them.  However, every
item of the array, as is mandated by the standard, is then
"assigned" the properly-cast value of zero.  For char's, this
means the nul character, although for pointers (an array of pointers, for
instance), this is the NULL value (which may or may not have the bit value
of all zero bits, but is always equal to the constant zero when cast).

 RS>    Hmm, slick.  I didn't know that.  I s'pose it's handy if you want 
 RS>    to initialize char arrays that aren't to be used as strings, or if 

This is its primary purpose...

 RS>    you have fixed-length fields that you'll use in routines like 
 RS>    strncpy() and printf("%2s",array).  I learn new things
in this echo 
 RS>    all the time.  :)

That's what it's here for, isn't it?  :-)

 DM> Nope - a good compiler should say nothing.
 RS>  
 RS>    Hmm.  I bet my compiler issues a warning.  

Have you tried it?


---
* 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™.