Address of an array

David via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 27 09:16:32 PDT 2016


On Thursday, 27 October 2016 at 16:13:34 UTC, David wrote:
> Hi
>
> The pointer (.ptr) of an array is the address of the first 
> array element. But what exactly is the address of the array? 
> And how is it used?
>
>   auto myArray = [5, 10, 15, 20, 25, 30, 35];
>   writeln("myArray.ptr: ", myArray.ptr); // myArray.ptr: 
> 7FFA95F29000
>   writeln("&myArray[0]: ", &myArray[0]); // &myArray[0]: 
> 7FFA95F29000
>   writeln("&myArray: ", &myArray); // &myArray: 7FFE4B576B10
>   writeln("*&myArray: ", *&myArray); // *&myArray: [5, 10, 15, 
> 20, 25, 30, 35]

Sorry that went to quickly ;-)

I observe for example that the even if the pointer is moved to 
another address the address of the (dynamic) array stays constant:

   auto shrink = myArray[0 .. $-1];
   writeln("shrink.ptr: ", shrink.ptr);
   writeln("&shrink: ", &shrink);

Note: myArray and shrink have the same ptr but a different address

   shrink ~= 100;
   writeln("shrink.ptr: ", shrink.ptr);
   writeln("&shrink: ", &shrink);

Note: After appending, shrink changes its ptr but its address 
stays the same.

Thanks for your help in advance.



More information about the Digitalmars-d-learn mailing list