DGL
https://delphigl.com/forum/

Setting a pointer your pointing to
https://delphigl.com/forum/viewtopic.php?f=19&t=9716
Seite 1 von 1

Autor:  Stucuk [ Di Jan 25, 2011 14:19 ]
Betreff des Beitrags:  Setting a pointer your pointing to

Is it possible to set a pointer that your pointing to? As in say i have a PChar called Bob and a Pointer called Harry, Harry points to Bob. Can i set Bob when all i have access to is Harry?

Autor:  Coolcat [ Di Jan 25, 2011 14:35 ]
Betreff des Beitrags:  Re: Setting a pointer your pointing to

Yes.

However, there is a difference between "Harry points to Bob" and "Harry points to the same location as Bob". In the first case Harry is a Pointer to a Pointer to a Char. Therefore you can modify the Bob-Pointer directly.

In the latter case Harry is just a pointer to a char. You can only modify the data directly, but not the location of the data.

Autor:  Stucuk [ Di Jan 25, 2011 14:43 ]
Betreff des Beitrags:  Re: Setting a pointer your pointing to

That sounds like a no rather than a yes. If i understand your post correctly then there isn't a way to set the actual PChar variable when you only have a pointer to the PChar variable(Well to allocate a PChar and have the original PChar variable point to it).

Autor:  Coolcat [ Di Jan 25, 2011 14:51 ]
Betreff des Beitrags:  Re: Setting a pointer your pointing to

Zitat:
That sounds like a no rather than a yes.

Sorry, "yes" in the first case and "no" in the second. I added a break above to make it more clear.

Edit: Note that a PChar variable is already a pointer to a Char.

Autor:  Stucuk [ Mi Jan 26, 2011 00:39 ]
Betreff des Beitrags:  Re: Setting a pointer your pointing to

Worked out how to do what i want. I realised that a Pointer is just a number, so if Harry points to Bob's own data(bit that holds the Memory Address bob points to) then Harry can modify Bob completely.

Code:
var
 Harry   : Pointer;
 Bob     : PChar;
begin
Bob := '';

Harry := Addr(Pointer(Bob));
PChar(Pointer(Cardinal(Harry^))) := 'Hello';

MessageBox(0,Bob,Bob,0); //Bob = Hello


In case your wondering im working on a GUI and an Editor for it. Im making it so all of the GUI classes register there editable(And savable) properties(To a class inside themselves) so that the editor can then access a list of a GUI Element's editable properties(With pointers directly pointing to each property) and easily edit them without having to code special cases in the editor to handle each property of each class(Only have to code stuff to handle the variable types instead).

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/