2D array question

Bernt Ribbum bernt at wolfen.cc.uow.oz
Mon Jul 2 14:51:31 AEST 1990


v8902477 at cc.nu.oz.au writes:


>	Hello,  could somebody please help me with the following? I'm attempting
>to create a two dimensional array at run time using calloc.  The array is an
>array of structures like this :
>
>	struct a {
>		int a1, a2, a3;
>	};
>
>And I'm using :
>
>	array = (struct a *) calloc(xdim*ydim, sizeof(struct a));

(correct so far)
However,

>	*((array+x+y*ydim).a1) = value;

... is not correct. (array+x+y*ydim) is still a pointer and it is thus
not valid to reference a structure member of it (ie .a1).

However, (*(array+x+y*ydim)) is a structure, so 

	(*(array+x+y*ydim)).a1 = value;

may be used, or better still:

	(array+x+y*ydim)->a1 = value;

(See K&R 2nd Ed. ch.6.2, pp.129-132)

     _____
      )   )                    Bernt Ribbum, Dept. of Elec. & Comp. Eng.
     /___/  _   __ __  _/_         University of Wollongong, PO.Box 1144
    /    ) /_) /  /  ) /                  Wollongong NSW 2500, AUSTRALIA
 (_/____/ (___/  /  /_/                       bernt at wolfen.cc.uow.edu.au



More information about the Comp.lang.c mailing list