Working with pointers/adresses

matheus matheus at gmail.com
Thu Jul 9 17:33:30 UTC 2020


On Thursday, 9 July 2020 at 17:24:33 UTC, matheus wrote:
> On Wednesday, 8 July 2020 at 20:33:39 UTC, Paul Backus wrote:
>> import std.stdio;
>>
>> void main()
>> {
>>     int i;
>>     readf("%d\n", i); // read a number
>>     ubyte* p = cast(ubyte*) i; // convert it to a pointer
>>     writeln(*p); // write the data at that address to the 
>> console
>> }
>>
>> Note that this program will almost certainly crash if you try 
>> to run it, since modern computers do not allow programs to 
>> read and write arbitrary memory locations.
>
> I wonder if the question was more like intended to display the 
> value using pointers, ie:
>
> import std.stdio;
>
> void main(){
>     int i;
>     readf("%d\n", i);
>     int *p = &i;
>     writeln(*p);
> }
>
> Because accessing arbitrary memory location seems very weird.
>
> Matheus.

Or maybe he wanted to do this:

import std.stdio;

void main(){
     int i;
     readf("%d\n", i);
     ulong address = cast(ulong)&i;
     ubyte* p = cast(ubyte*)address;
     writeln(*p);
}

Matheus.


More information about the Digitalmars-d-learn mailing list