Array access via pointer

Simen kjaeraas simen.kjaras at gmail.com
Sun May 30 14:12:06 PDT 2010


Robert <robert.muench at robertmuench.de> wrote:

> On 2010-05-30 22:16:55 +0200, Pelle <pelle.mansson at gmail.com> said:
>
>> &data[0] and &data are very different things, if you have a dynamic  
>> array.
>
> Yes, that's what I found out too. What's the exact difference?

Going again with the C code:

typedef struct array {
   int* data;
   int length;
};

You would use an array like this:

void foo( ) {
    array arr;
    arr.ptr = malloc(32);
    arr.length = 8;
}

Now, as you can probbly see, &arr would give the pointer to the
struct, not to the data. Basically, a pointer to pointer to int,
rather than the pointer to int you want.

-- 
Simen


More information about the Digitalmars-d mailing list