我试图在循环中增加简单记录的数字字段:
program Progrecords;
{$mode objfpc}
Type
Progrec = Record
pname : string;
pnum : integer;
end;
var
Progs : Array [1..3] of Progrec;
aprog, pp: Progrec;
function getrec(ppname: string): Progrec;
begin
for pp in Progs do begin
if pp.pname = ppname then
result := pp ;
end;
end;
begin
aprog.pname := 'abc'; aprog.pnum := 4; Progs[1] := aprog;
aprog.pname := 'def'; aprog.pnum := 6; Progs[2] := aprog;
for pp in Progs do writeln('Name: ',pp.pname, '; Num: ', pp.pnum);
{increment nums in both recs: }
pp := getrec('abc'); pp.pnum := pp.pnum + 1;
pp := getrec('def'); pp.pnum := pp.pnum + 1;
for pp in Progs do writeln('Name: ',pp.pname, '; Num: ', pp.pnum);
end.
代码可以正确编译并运行,但是number字段没有递增。
输出为:
$ fpc rnrecords_so.pas && ./rnrecords_so
Free Pascal Compiler version 3.0.0+dfsg-11+deb9u1 [2017/06/10] for x86_64
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling rnrecords_so.pas
Linking rnrecords_so
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
31 lines compiled, 0.1 sec
Name: abc; Num: 4
Name: def; Num: 6
Name: ; Num: 0
Name: abc; Num: 4 <<<<<<< NOTE: NUMBERS HAVE NOT BEEN INCREMENTED;
Name: def; Num: 6
Name: ; Num: 0
问题出在哪里,如何解决?感谢您的帮助。