how do you dynamically allocate multidimensional arrays?
Alexis Dimitriadis
alexis at reed.UUCP
Mon Apr 29 15:18:12 AEST 1985
In article <669 at burl.UUCP> geoff at burl.UUCP (geoff) writes:
> ... while 'l' correctly packs the data into 8
>contiguous words, 'p' loads the data into every other 2-word segment. If
>I increase the column dimension of p to 3, it loads it into every third
>2-word segment.
>What am I doing wrong?? How do I do it right??
>
>int j[100]; /* just getting some initialized space */
>main()
>{
> struct fred {
> int i;
> int j;
> };
> struct fred (*p)[2][2];
> struct fred (l)[2][2];
> int i, *ip;
>
> p = (struct fred (*)[2][2])j;
That's easy! The way you declare p, it is a pointer to a two-by-two
array of fred. Since p[n] is equivalent to *(p+n), the first index
increments by the size of the pointed object--In fact you could use
p[x][y][z] without getting a diagnostic from the compiler.
The correct way to do this would be to declare
struct fred l[2][2], *p[2];
Then you could use p[n], to access the n-1st 8 byte block of memory
starting at *p, as in p[n][1].i (and not p[n][1]->i !)
Alexis Dimitriadis
--
_______________________________________________
As soon as I get a regular job, the opinions expressed above
will attach themselves to my employer, who will never be rid of
them again.
alexis @ reed
...ihnp4!{harvard|tektronix}!reed
...decvax!tektronix!reed
...teneron!reed
More information about the Comp.lang.c
mailing list