{$MODE OBJFPC }
program PointersToConstantCharArrays;
const
constchar = 'a character constant array';
aconstarray : array[0..1] of pchar = ('first', 'second');
type
rec = record
intfield : integer;
charfield : pchar;
end;
const
// this works because there is no array of pointers to the character array
// i.e, constchar is itself the pointer to the character array.
recconst : rec = (intfield : 0; charfield : constchar);
const
// this does NOT work
// it should NOT force the use of the @ operator, it won't compile without it
// and with it, the output is wrong. This results in charfield holding a
// pointer to a pointer to characters instead of a pointer to characters.
// IOW, charfield is a ppchar instead of, what it should be, a pchar.
recconst2 : array[0..1] of rec
= (
(intfield : 0; charfield : @aconstarray[0]),
(intfield : 0; charfield : @aconstarray[1])
);
var
AnArray : array[0..31] of char;
begin
writeln(recconst.charfield);
// the following two statements output garbage because charfield is a pointer
// to a pointer to characters. charfield is NOT a pointer to characters which
// is what writeln expects.
writeln(recconst2[0].charfield); // outputs garbage
writeln(recconst2[1].charfield);
AnArray[0] := 'h'; AnArray[1] := 'e'; AnArray[2] := 'l';
AnArray[3] := 'l'; AnArray[4] := 'o'; AnArray[5] := #0;
// this assignment should not be possible
aconstarray[0] := AnArray; // AnArray isn't a pointer to a string of
// character constants. The compiler should NOT
// allow this but, it has to.
writeln(aconstarray[0]);
// it is possible to write to aconstarray[0][0]
aconstarray[0][0] := 'f';
writeln(aconstarray[0]);
// but the same thing on [1][0] will result in a SIGDEV
aconstarray[1][0] := #0;
end.