Adding array of char?
frank at g-n-u.de
Wed Mar 12 17:48:18 CET 2003
Peter N Lewis wrote:
> At 14:28 -0800 11/3/03, Russell Whitaker wrote:
> >adding 3 more lines of code should answer your question:
> > > program test;
> >> type
> >> Str255 = array[0..255] of char;
> > > var
> > > a,b: Str255;
> >> begin
> > a := 'hello ';
> > b := 'world';
> >> a := a + b;
> > writeln( a );
> > > end.
> Amusingly, yes, that is originally what I had. But since it only
> printed "hello" when I ran it, I presumed it was not what I expected.
> Indeed, when I run the above program, it outputs:
> zany:~/unix/c% ./testpas2
> In fact, it seems to output "hello" followed by 256-5 spaces,
> followed by a newline.
Such arrays have fixed-length (no explicit length indicator). So
what does the compiler do when assigning a shorter string? It pads
with spaces. EP 6.4.6:
: At any place where the rule of assignment-compatibility is used to
: require a value of the canonical string-type to be
: assignment-compatible with a fixed-string-type or the char-type,
: the canonical string-type value shall be treated as a value of the
: fixed-string-type whose components in order of increasing index
: shall be the components of the canonical-string-type value in
: order of increasing index followed by zero or more spaces.
So after the first assignment a already contains `hello' plus 251
spaces. Appending more to it isn't possible, to that's what's
Frank Heckenbach, frank at g-n-u.de, http://fjf.gnu.de/, 7977168E
GPC To-Do list, latest features, fixed bugs:
GPC download signing key: 51FF C1F0 1A77 C6C2 4482 4DDC 117A 9773 7F88 1707
More information about the Gpc