The "result" value of the function is OK (0) till just before the "exit" is excuted. After the "exit" the value has changed to -1, see comment in the source code. Replacing "exit" with "break" does not change the result.
Thanks in advance!
Source code:
Code: Select all
program TestCompareStr;
{ Declarations section }
var Str1, Str2: string[10];
Reslt: integer;
function CompareStr(var S1,S2:string):integer;
// compares S1 to S2, with case-sensitivity. The return value is less than 0 if S1 is less than S2, 0 if S1 equals
// S2, or greater than 0 if S1 is greater than S2. The compare operation is based on the 8-bit ordinal value of
// each character. Terminating char (0) is taken into account thus allowing to compare string lengths, if this is
// the only difference.
var Ptr1, Ptr2: ^Char;
begin
Result := 0;
Ptr1 := @S1;
Ptr2 := @S2;
while true do
begin
Result := Ptr1^ - Ptr2^;
if (Result <> 0) or (Ptr1^ = 0) or (Ptr2^ = 0) then exit; // "result" is 0 before the "exit" is executed
inc(Ptr1);
inc(Ptr2);
end;
end; // here "result" has been changed to -1
begin
{ Main program }
Str1 := '123';
Str2 := '123'; // 2 equal strings
Reslt := CompareStr(Str1, Str2); // Reslt should be 0, but is -1 actually
end.