Array access via pointer

Robert robert.muench at robertmuench.de
Sun May 30 14:54:29 PDT 2010


On 2010-05-30 23:12:06 +0200, "Simen kjaeraas" <simen.kjaras at gmail.com> said:

> 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.

Ok, I thought that the structure was a bit more flat like:

typedef struct array {
	int length;
	int[1..length] data;
}

Avoiding one indirection as it could be assumed that the 
memory-allocator / GC will return a continous piece for the array. But 
of course resizing and reallocation would be a bit more complicated.

-- 
Robert M. Münch
http://www.robertmuench.de



More information about the Digitalmars-d mailing list