Question about arrays

Ali Çehreli acehreli at yahoo.com
Sat Apr 21 15:25:44 PDT 2012


On 04/21/2012 03:05 PM, Stephen Jones wrote:
 > My C programming lies in cobwebs but from memory an array was a pointer
 > to the zeroth index of a set of uniformly sized chunks of memory.

Yes and no. :) What you are describing is the feature where an array 
decays to what you describe.

But there is also the concept of array in C, which is a collection of 
elements side by side. When you apply sizeof to such a thing, you get 
the size of the whole thing. So, the whole thing is the array.

But you are right, when passed to functions, they decay to "pointer to 
first element."

 > I am
 > perplexed to find that in D a call to an array (of float vertices for
 > example) cannot be accomplished by handing &v to functions that need the
 > zeroth index. Is this because D holds a pointer to an array object and
 > the zeroth index is accessed (via compiler background work) to &v[0]?

In D, arrays are what they should have been in C :). A pointer and a 
size. Something the equivalent of the following (for the int type):

struct int_Array
{
     int * elements;
     size_t number_of_elements;
}

Correction: That is a slice (aka dynamic array). There is also the 
fixed-length arrays (aka static arrays) in D.

If you pass the address of a slice, you are passing the address of such 
a struct variable. So don't bother with the address at all, just pass 
the array object by value.

Ali



More information about the Digitalmars-d-learn mailing list