In Delphi:
How do I get the address (0x2384293) a pointer points to?
var iValue := Integer;
iptrValue := PInteger;
implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
iptrValue := @iValue;
iValue := 32342;
//Should return the same value:
Edit1.Text := GetAddressOf(iptrValue);
Edit2.Text := GetAddressOf(iValue);
So what is GetAddress in reality 🙂
To get the address of something, use the
@operator or theAddrfunction. You’ve already demonstrated correct use of that. You got the address ofiValueand stored it iniptrValue.To display an address, you can use the
Formatfunction to convert a pointer value into a string. Use the%pformat string:That will display the address of the
iptrValuevariable, then the address stored in that variable, and then the value stored at that address.The
iptrValuevariable declaration reserves some bytes in memory and associates a name with them. Suppose the address of the first byte is$00002468:The
iValuedeclaration reserves another piece of memory, and it will probably be adjacent to the previous declaration’s memory. SinceiptrValueis four bytes wide, the address ofiValuewould be$0000246C:The boxes I’ve drawn are empty for now because we haven’t discussed what values those variables hold. We’ve only discussed the variables’ addresses. Now to the executable code: You write
@iValueand store the result iniptrValue, so you get this:Finally, when you display the results of the
Formatfunction from above, you would see this value: