Why & array! = & Array [0]?

In C:

int a[10];
printf("%p\n", a);
printf("%p\n", &a[0]);

Productivity:

0x7fff5606c600
0x7fff5606c600

This is what I expect. Now, in D, I'm trying to do this (obviously not to use, just cheat):

int[] slice = [...];
writeln(&slice);
writeln(&slice[0]);

Productivity:

7FFF51600360
10E6E9FE0

Why is the difference? Looks like a completely different segment of memory. (Although it occurred to me that perhaps the arrays in D are not just contiguously allocated ints?)

+4
source share
3 answers

in D, an array is essentially a structure with a pointer and a length field and is considered as such

to get the address of the first element, you can query the ptr field

+12
source

"", , , .

int[] slice = [...];
writeln((cast(void*) slice);
writeln(&slice[0]);

, .

"C" http://c-faq.com/aryptr/aryptrequiv.html

int[] slice = [...];
int* ptr = &slice[0];

writeln(cast(void*) slice);
writeln(&slice[0]);

// value access
writeln(slice[0]);
writeln(*ptr);
writeln(ptr[0]);
+3

- D C. D , C - . D NULL, . , D- , C.

import std.stdio;

int main() {
  // static array
  int[10] sarr;
  writeln(sarr.length);
  writeln(&sarr);
  writeln(&sarr[0]);

  // dynamic array
  int[] darr = [1, 2, 3];
  writeln(darr.length);
  writeln(&darr);
  writeln(&darr[0]);

  // These are all the same
  writeln(darr.ptr);
  writeln(cast(void*) darr);
  writeln(&darr[0]);

  return 0;
}

( DPaste: http://dpaste.dzfl.pl/f708d945)

+3

All Articles