Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!munnari.oz.au!metro!wolfen!bernt From: bernt@wolfen.cc.uow.oz (Bernt Ribbum) Newsgroups: comp.lang.c Subject: Re: 2D array question Message-ID: <6311@wolfen.cc.uow.oz> Date: 2 Jul 90 04:51:31 GMT References: <3336.268f44b2@cc.nu.oz.au> Organization: Uni of Wollongong, NSW, Australia Lines: 38 v8902477@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@wolfen.cc.uow.edu.au