Results 1 to 4 of 4

Thread: The dereference operator is weird

  1. #1

    The dereference operator is weird

    Hey delphi users,

    I just discovered something odd. Check this out:

    Code:
    type
      PPPPMyRec = ^PPPMyRec;
      PPPMyRec = ^PPMyRec;
      PPMyRec = ^PMyRec;
      PMyRec = ^TMyRec;
      TMyRec = record
        Str: String;
        Int: Integer;
      end;
    
    procedure TForm1.Button5Click(Sender: TObject);
    var
      P: PMyRec;
      PP: PPMyRec;
      PPP: PPPMyRec;
      PPPP: PPPPMyRec;
    begin
      New(P);
      P^.Str := 'test';
      P^.Int := 5;
    
      PP := @P;
      PPP := @PP;
      PPPP := @PPP;
    
      //A pointer to a record... only one dereference needed
      ShowMessage(P^.Str);
    
      //A pointer to a pointer to a record... STILL ONLY ONE DEREFERENCE NEEDED!!
      ShowMessage(PP^.Str);
    
      //triple pointer... now we need to add an extra dereference operator
      ShowMessage((PPP^)^.Str);
    
      //Yet another extra ^
      ShowMessage(((PPPP^)^)^.Str);
    
      Dispose(P);
    end;
    I think this is disturbing. In the second case, it's not possible to set the value of P by using PP^, because it dereferences 2 times at once. Is there a way to do that?

    I hope fpc is more consistent with these things.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  2. #2
    In Delphi it'll try to automatically dereference if you don't do it

    Eg. this would work. It wouldn't work in FPC, unless you use mode Delphi
    Code:
      New(P);
      P.Str := 'test';
      P.Int := 5;
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3
    Thanks for your response.

    Is it actually possible that PPPMyRec is automaticly dereferenced or does it only do second-degree pointers?

    Delphi is a great IDE, but "automation" like this actually suck pretty badly. It makes things non-intuitive.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  4. #4
    Quote Originally Posted by chronozphere View Post
    I think this is disturbing. In the second case, it's not possible to set the value of P by using PP^, because it dereferences 2 times at once. Is there a way to do that?
    Cast the typed Pointer to a Pointer and evrything is wonderful.
    Code:
    var
      P: PMyRec;
      AnotherP: PMyRec;
      PP: PPMyRec;
    //  PPP: PPPMyRec;
    //  PPPP: PPPPMyRec;
    begin
      New(P);
      P^.Str := 'test';
      P^.Int := 5;
      New(AnotherP);
      AnotherP.Str := 'AnotherTest';
      AnotherP.Int := 6;
    
      PP := @P;
    //  PPP := @PP;
    //  PPPP := @PPP;
    
      ShowMessage(P.Str);
      Dispose(P);
      Pointer(PP^) := AnotherP;
      ShowMessage(P.Str);
    
      Dispose(AnotherP);
    end;

    Quote Originally Posted by chronozphere View Post
    Is it actually possible that PPPMyRec is automaticly dereferenced or does it only do second-degree pointers?
    i dunno, but normaly i always notice if something is going wrong.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •