Mail Archives: djgpp/1999/09/29/21:27:22
> On Wed, 29 Sep 1999 02:57:07 +0300, "stefan fröberg"
> <traveler AT netti DOT fi> 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/
- Raw text -