CBFalconer wrote:
loop lopy wrote:
How does writeln knows its the end of a string? I have the following code:
String = packed array[1..256] of char;
var Test: String;
begin Test:='Hello world'; writeln (Test); Test[8] := Test[12]; writeln (Test); end.
The output is:
Hello world
Hello w rld
How does writeln knows where to stop printing the string?
It doesn't. "Writeln(something)" is specified as shorthand for "write(something); writeln".
However write knows that you specified the string to be an array of 256 chars, so it outputs 256 chars (unless told to truncate by a field specification). Those chars have somehow become blanks in your system, and that part is not standard. The assignment to test evidently has been arranged to append blanks, and it also appears as if the write operation has been arranged to elide trailing blanks, at least when the line is ended by the writeln operation. Strictly speaking the assignment to test should have raised an error.
No, all elements of `Test' have well-defined value. It is standard: `Test' is padded with blanks to the full length (so all positions from 12 to 256 contain blanks). GPC `write' strips trailing blanks. You can try adding something like:
Test[70] := 'X';
before `writeln'.
For background: Standard Pascal was developed at the end of sixties and in early sevenites. At that time files in computers were frequently stored as a sequence of fixed length records-lines. The usual practice was to pad lines with blanks to full length (and truncate longer lines). When using variable lenght lines a conversion was needed: one had to strip trailing blanks from fixed length lines. Standard Pascal does not specify what happens to the files, to allow both fixed length lines and variable length lines.
Note: popular systems now use variable length lines, but Pascal Standard (ISO 7185) is from 1983, and the explicit goal was to describe original language, without adding extensions. The is newer standaed, Extended Pascal (ISO 10206) which among other extensions added support for true variable length strings. However loop lopy using `--standard-pascal' options explicitly requested GPC to reject newer additions.
a . d . jensen wrote:
If it's a Pascal string, the first byte shows the length of the string. In other words, this assignment:
myS:= 'Test';
puts the value of 4 in the first byte. Your "test[8]:= test[12];" simply grabbed whatever was hanging in memory at the location one past the end of your string and copied it over.
No, you write here about `short strings'. ATM GPC does not support short strings (there are plans to add them). The original program uses just blank padded array. GPC supports Extended Pascal strings, but those use separate (hidden) integer-sized length field. So also for EP strings access to byte 0 is invalid.