From: "Michael Stewart" Newsgroups: comp.os.msdos.djgpp Subject: Re: A Structured Problem ? Date: Mon, 19 Jul 1999 12:21:16 +0100 Organization: (Posted via) Netcom Internet Ltd. Message-ID: <7mv1nr$cpg$1@taliesin.netcom.net.uk> References: <7mv0te$gq2$1 AT news6 DOT svr DOT pol DOT co DOT uk> NNTP-Posting-Host: hgty.capgemini.co.uk X-Trace: taliesin.netcom.net.uk 932383291 13104 194.42.240.2 (19 Jul 1999 11:21:31 GMT) X-Complaints-To: abuse AT corp DOT netcom DOT net DOT uk NNTP-Posting-Date: 19 Jul 1999 11:21:31 GMT X-Newsreader: Microsoft Outlook Express 4.72.3155.0 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0 Lines: 63 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Paul J G wrote in message <7mv0te$gq2$1 AT news6 DOT svr DOT pol DOT co DOT uk>... >Hi Guys, I really need some help big time on this one, can anyone help put >me out of my misery ?. I'll attempt to. >Right, I have a structure in my program: > >struct { float xpoint[9],float ypoint[9] } shape[10]; > >Fine it works, everythings OK, but now I want to be able to increase the >amount of data used by xpoint and ypoint while still using the SHAPE >identifier. >eg. shape[1] will be xpoint[9],ypoint[9], shape[2] will be >xpoint[25],ypoint[25], > shape[3] will be xpoint[3],ypoint[3] etc..... >However I cannot just allocate a big variable as shape[1] could be just 3 >and >shape[2] could be 8000. >I still want to be able to use my - shape[ whatever ].xpoint[ whatever ] - >code. >Not knowing what the varibles xpoint, ypoint, will be at compilation time >they must be dynamically >allocated, as the file length for each shape could vary. >The idea being that I can load my 3d objects into the shape varibles. Something like this ? struct { int num_pts; float *xpoint; float *ypoint; float *zpoint; } shape[10]; void set_size (int shape_no, int num_points) { if (shape_no < 0 || shape_no >= 10) return; num_pts = num_points; shape[shape_no].xpoint = (float *) malloc (sizeof (float) * num_points); shape[shape_no].ypoint = (float *) malloc (sizeof (float) * num_points); shape[shape_no].zpoint = (float *) malloc (sizeof (float) * num_points); } (Note: You should check malloc worked) ... set_size (0, 50); for (int i = 0; i < 50; i++) { shape[0].xpoint[i] = get_x (i); shape[0].ypoint[i] = get_y (i); shape[0].zpoint[i] = get_z (i); } ... This'll be fine if you will not have more than 10 shapes, if you want more then you can allocate that dynamically. None of the above has been tested, so there may be errors. Michael Stewart