From: "Damian Yerrick" Newsgroups: comp.os.msdos.djgpp Subject: Re: HELP ! (returning structures) Date: Wed, 29 Sep 1999 16:56:30 -0500 Organization: Rose-Hulman Institute of Technology Lines: 68 Message-ID: <7su21s$9rk$1@solomon.cs.rose-hulman.edu> References: <7srkeu$3kh$1 AT tron DOT sci DOT fi> <37f173b9 DOT 2534667 AT newsserver DOT cc DOT monash DOT edu DOT au> NNTP-Posting-Host: yerricde.laptop.rose-hulman.edu X-Trace: solomon.cs.rose-hulman.edu 938642300 10100 137.112.205.146 (29 Sep 1999 21:58:20 GMT) X-Complaints-To: news AT cs DOT rose-hulman DOT edu NNTP-Posting-Date: 29 Sep 1999 21:58:20 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com > On Wed, 29 Sep 1999 02:57:07 +0300, "stefan fröberg" > wrote: > > >How can functions return whole structures ? > > > >Example: > > > >struct DATE > >{ > > unsigned char day_of_week; > > unsigned short year; > > unsigned char month; > > unsigned char day; > >} > > > >extern struct DATE GetDate(void); > > > >int main(void) > >{ > > struct DATE today; > > today = GetDate(); > > printf("%u\n",today.day_of_week); > > printf("%U\n",today.year); > > printf("%u\n",today.month); > > printf("%u\n",today.day); > >} Davin responded: > struct DATE GetDate(void) > { > struct DATE x; > x.day_of_week = GetDayOfWeek(); > x.year = ... > .... > return x; > } Davin, your solution is IMHO not the best. Passing structures on the stack is inefficient. The best way to return a structure is to fill in a blank structure pointed to in the arguments. void GetDate(struct DATE *the_date) { x->day_of_week = GetDayOfWeek(); x->year = ... .... } int main(void) { struct DATE today; GetDate(&today); printf("%u\n",today.day_of_week); printf("%U\n",today.year); printf("%u\n",today.month); printf("%u\n",today.day); } Davin, I challenge you to find one C library function that returns a struct on the stack. :-) Damian Yerrick http://pineight.webjump.com/