program lengthofcharacterarrayconstants2;
const
a = 'abcdefghi';
l = length(a);
type
TINDEXES = 0..3;
const
// if any string is made longer than the currently longest string then the
// constant "Longest" needs to be updated to be the length of the longest
// string.
const_1 = 'this is const 1 - a';
const_2 = 'this is const 2 - ab';
const_3 = 'this is const 3 - cba';
const_4 = 'this is const 4 - 12';
const
// comment here informing that Longest should be set to the length of the
// longest string in the set of const_xx
Longest = length(const_3); // const_3 randomly chosen for
// the example's sake
constants : array[TINDEXES] of pchar = (const_1, const_2, const_3, const_4);
const
ADDITIONAL_SPACE_NEEDED = 16; // for additional text in real life app.
// determined by a combination of text.
space = ' ';
comma = ',';
colon = ':';
// move wants variables for the above - I don't use move so that won't be a
// problem. The variables below are only for this example.
spacev : pchar = space;
commav : pchar = comma;
colonv : pchar = colon;
type
// it accepts this!! -> excellent - this solves the problem!
TEXT_BUFFER = array[0 ..
(2 * Longest) + ADDITIONAL_SPACE_NEEDED] of char;
var
TheBuffer : TEXT_BUFFER;
Index : sizeint = 0;
var
b : array[0..l] of char;
i : integer;
begin
// code below is similar/equivalent (and much simpler) to what I'm doing but,
// it is representative. Not included in the code below is code to ensure
// that index doesn't go beyond high(TheBuffer). With the definitions based
// on the length of the character arrays, it should never happen but,
// ensuring it doesn't, is so cheap, it's worth including in a real program.
// initialize the buffer with spaces and null terminate it.
FillChar(TheBuffer, sizeof(TheBuffer), space);
TheBuffer[high(TheBuffer)] := #0;
// now the buffer is ready - use it :-)
move(const_2, TheBuffer, length(const_2));
inc(index, length(const_2));
// place a comma in there
move(commav^, TheBuffer[Index], sizeof(comma));
inc(index, sizeof(comma));
// a space after a comma. could do space_comma in one shot but
// this example is just to show the mechanics.
move(spacev^, TheBuffer[Index], sizeof(space));
inc(index, sizeof(space));
// now put the next string in the buffer
move(const_4, TheBuffer[Index], length(const_4));
inc(index, length(const_4));
// null terminate it
TheBuffer[Index] := #0;
// write it out
writeln(TheBuffer);
move(a[1], b[0], l);
writeln(a);
writeln(l);
writeln(b[l-1]);
for i := low(constants) to high(constants) do writeln(constants[i]);
readln
end.