要指针操作并非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;哪错了。。?
大伙看看是什么原因吧。。?
难道不能这么写吗?

解决方案 »

  1.   

    ShowMessage( IntToStr(PRec(PChar(@(p^.a))+sizeof(integer)).b));  //得不到正确的值
    改为:
    ShowMessage( IntToStr(PRec(PChar(@(p^.a))+sizeof(integer))^.b));  //得不到正确的值
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    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它就偏移一点。我想你应该明白了吧。
      

  3.   

    TRec = record
        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。
      

  4.   

    TRec = record
        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时的情况。
      

  5.   

    应该是:
    ShowMessage( IntToStr(PRec(PChar(@(p^.a))).b));或者
    ShowMessage( IntToStr(PRec(PChar(@(p^.b))-sizeof(integer)).b));因为p^.a的地址就是PRec的首地址了。—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  6.   

    我明明有回复过的,怎么会没掉了呢。要了解答案的请看。
    http://www.delphibbs.com/delphibbs/dispq.asp?lid=1569537
    http://www.delphibbs.com/delphibbs/dispq.asp?lid=1571430
      

  7.   

    写得这么复杂。:(TRec = record
        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;
      

  8.   

    很简单的问题,你的地址越界。ShowMessage( IntToStr(PRec(PChar(@(p^.a))+sizeof(integer)).b));  //废话!当然得不到正确的值PChar(@(p^.a))+sizeof(integer) //得到的是b的地址 假设它为AA而PRec(AA)进行了数据类型转换,它的作用是将AA地址起始的4字节数字当成TRec类的a元素,之后连续4字节的数字为b元素的数字。可想而知,这之后连续的4字节数字是不知道的 没赋值 当然不是b的数字了,而如果该为:ShowMessage( IntToStr(PRec(PChar(@(p^.a))+sizeof(integer)).a));它就显示的是6,刚好是原来b元素的值,现在知道了吗?
      

  9.   

    你得不到正确的值是因为需要b的值的地址不是真真的b的地址!
    risingsoft(一苇渡江) 描述的对!但是改的结果也
    不过在程序也可以这样做:
    因为在a的地址增加了a的长度后,地址为b的地址,而b的地址中存贮了b的值,且类型为整型,所以可以用Pinteger来取b地址的值。因此下面写的代码为:
    showmessage(IntToStr(PInteger(Integer(@(p^.a))+sizeof(Integer))^));