要指针操作并非p^.b
=======================TRec = record
a: integer;
b: integer;
end;
PRec= ^TRec;
=============
procedure TForm2.Button1Click(Sender: TObject);
var
t:TRec;
p:PRec;
i:integer;
pa,pb:^integer;
begin
with t do
begin
a:=8;
b:=6;
end; p:=@t; pa:=@(p^.a);
pb:=@(p^.b); ShowMessage(IntToStr((PChar(pb)-PChar(pa)))); //4 说明地址是连续的。
ShowMessage( IntToStr(PRec(PChar(@(p^.a))+sizeof(integer)).b)); //得不到正确的值
end;哪错了。。?
大伙看看是什么原因吧。。?
难道不能这么写吗?
=======================TRec = record
a: integer;
b: integer;
end;
PRec= ^TRec;
=============
procedure TForm2.Button1Click(Sender: TObject);
var
t:TRec;
p:PRec;
i:integer;
pa,pb:^integer;
begin
with t do
begin
a:=8;
b:=6;
end; p:=@t; pa:=@(p^.a);
pb:=@(p^.b); ShowMessage(IntToStr((PChar(pb)-PChar(pa)))); //4 说明地址是连续的。
ShowMessage( IntToStr(PRec(PChar(@(p^.a))+sizeof(integer)).b)); //得不到正确的值
end;哪错了。。?
大伙看看是什么原因吧。。?
难道不能这么写吗?
解决方案 »
- 请问C++的 union struct 应该怎么理解?
- DELPHI数据转换Int64 to String
- 針式打印機如何如何實現連續出紙
- 快速将工程所有单元内的字符替换的方法
- 关于‘动态创建窗体’问题
- 用delphi写的程序如何打包?谁有打包软件
- Insert 语句 与 Set identity_insert 的疑问!!!!
- 我的老板说,dbgrid的滚动条不好用!
- Com 高手请进:如何实现方法(和属性)的“重载”?(可加分)
- WIN98程序升至WIN2000后为啥老是弹出CPU窗口呢?急!
- 代码转换的问题,请大家帮帮我。总是提示STAck Overflow
- 主program部分的变量和函数的调用
改为:
ShowMessage( IntToStr(PRec(PChar(@(p^.a))+sizeof(integer))^.b)); //得不到正确的值
var
t:TRec;
p,tp:PRec;
i:integer;
pa,pb:^integer;
br:Integer;
begin
new(p);
new(tp);
with t do
begin
a:=1;
b:=2;
end;
p:=@t;
pa:=@(p^.a);
pb := @(p^.b);
tp:= PRec(PChar(@(p^.b)));
//tp:= PRec(PChar(@(p^.a)));
br := tp.b;
Self.Caption := IntToStr(br);
end;说明一下:在我这儿地址在哪儿呢,指针指向b,那么结果回事什么呢?结果是长长的一窜数,为什么呢,因为现在虽然指针指向b,但是tp.b究竟如何执行的呢,根据你的记录结构,他又指向到当前地址再加上那么一小段地址的地方去了,结果当然大出所料。如果你将br := tp.b;改成br := tp.a 结果会如何呢,哦,原来他怎么是2呢,不要奇怪,按照你结构的定义,它就去取现在指针指向的值。再把tp:= PRec(PChar(@(p^.b)));改成tp:= PRec(PChar(@(p^.a)));,你会发现他们都能取到正确的值。
说了这么多,实际上说白了,在这里,当指针指p向a时,执行p.a指针实际上不动,执行p.b它就偏移一点。我想你应该明白了吧。
a: integer;
b: integer;
c:Integer;
end;
PRec= ^TRec;procedure TForm1.Button1Click(Sender: TObject);
var
t:TRec;
p,tp:PRec;
i:integer;
pa,pb,pc:^integer;
br:Integer;
begin
new(p);
new(tp);
with t do
begin
a:=1;
b:=2;
c := 3;
end;
p:=@t;
pa:=@(p^.a);
pb := @(p^.b);
pc := @(p^.c);
tp:= PRec(PChar(@(p^.b)));
//tp:= PRec(PChar(@(p^.a)));
br := tp.b;
Self.Caption := IntToStr(br);
end;想一想:现在br为多少,它应该是3。
a: integer;
b: integer;
c:Integer;
d:Integer;
end;
PRec= ^TRec;procedure TForm1.Button1Click(Sender: TObject);
var
t:TRec;
p,tp:PRec;
i:integer;
pa,pb,pc,pd:^integer;
br:Integer;
begin
new(p);
new(tp);
with t do
begin
a:=1;
b:=2;
c := 3;
d := 4;
end;
p:=@t;
pa:=@(p^.a);
pb := @(p^.b);
pc := @(p^.c);
pd := @(p^.d);
tp:= PRec(PChar(@(p^.b)));
//tp:= PRec(PChar(@(p^.a)));
br := tp.c;
Self.Caption := IntToStr(br);
end;
这儿的结果应该是4,实际上tp.a、tp.b、tp.c得到的值实际上相当于指针指向a时tp.b、tp.c、tp.d的值,而指针指向b时tp.d的值就是一个随即值,同理可以类推,指针指向c,d时的情况。
ShowMessage( IntToStr(PRec(PChar(@(p^.a))).b));或者
ShowMessage( IntToStr(PRec(PChar(@(p^.b))-sizeof(integer)).b));因为p^.a的地址就是PRec的首地址了。—————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
—————————————————————————————————
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1569537
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1571430
a: integer;
b: integer;
end;
PRec= ^TRec;var
rec: TRec;
P: PInteger;
begin
rec.a := 10;
rec.b := 20;
P := @rec;
ShowMessage(IntToStr(P^));
Inc(P);
ShowMessage(IntToStr(P^));
end;
risingsoft(一苇渡江) 描述的对!但是改的结果也
不过在程序也可以这样做:
因为在a的地址增加了a的长度后,地址为b的地址,而b的地址中存贮了b的值,且类型为整型,所以可以用Pinteger来取b地址的值。因此下面写的代码为:
showmessage(IntToStr(PInteger(Integer(@(p^.a))+sizeof(Integer))^));