passing structures

Dale Cook cdm at gem-hy.Berkeley.EDU
Tue Oct 16 02:59:12 AEST 1990


In article <241 at motto.UUCP>, andrew at motto.UUCP (Andrew Walduck) writes:
|> okay folks here goes...
|> 
|> In the new ANSI standard, we can now pass (and return) a structure by
|> value. Like so...(fragment follows)
|> 
|> -----------------------------------------------------------------------
|> #include <stdio.h>
|> 
|> typedef struct complex {
|>                          int real;
|>         		 int imag; 
|>                        } complex;
|> 
|> complex add(complex, complex); /* function prototype for complex add */
|> 
|> int main(void)
|> {
|>   complex result, a, b;
|> 
|>   a.real = 5; a.imag = 6; /* 5+6i */
|>   b.real = 8; b.imag = 3; /* 8+3i */
|> 
|>   result = add(a,b); /* should be 13+9i */
|> 
|>   printf("result : %i+%ii\n", result.real, result.imag);
|> }
|> 
|> complex add(complex a, complex b)
|> {
|>   complex result;
|> 
|>   result.real = a.real + b.real;
|>   result.imag = a.imag + b.imag;
|>   
|>   return result;
|> }
|> ----------------------------------------------------------------------- 
|> Now, that should work right?!!
|> 
|> Now, here's the problem...what if I wanted to pass a constant structure
|> to add! For example I wanted to add 5+8i to a:
|> 
|> The call to add would look like this??
|> 
|> result = add(a,{5,8});
|> 
|> But this isn't supported by ANSII! There's no way to pass a structure
|> as a parameter! It should be do-able, the prototype exists, so the 
|> types can be punned appropriately...any idea why it wasn't? No prior-art?
|> 
|> Any idea how I can suggest this to the committee?
|> 
|> Thanx
|> Andrew Walduck
|>  ______________________________________________________________________
|> |andrew at motto.UUCP | I wasn't aware that Ada was useful - Henry Spencer|

Forgive me for being dense, but why would one want to do this at all?
First of all, it's bad form to bury hard numbers in code.
Second, passing structures by value might be ok for small structures, but 
as the size increases, your stack requirements do as well.  Especially
if you're using recursive routines...

--- Dale Cook

Neither the United States Government nor the Idaho National
Engineering Laboratory nor any of their employees, makes any warranty,
expressed or implied, or assumes any legal liability or responsibility
for the accuracy, completeness, or usefulness of any information,
product, or process disclosed, or represents that its use would not
infringe privately owned rights.  Reference herein to any specific
commercial products, process, or service by trade name, trademark
manufacturer, or otherwise, does not necessarily constitute or imply
its endorsement, recommendation, or favoring by the United States
Government or the Idaho National Engineering Laboratory.  The views and
opinions of authors expressed herein do not necessarily state or reflect
those of the United States Government nor the Idaho National Engineering
Laboratory, and shall not be used for advertising or product endorsement
purposes.



More information about the Comp.lang.c mailing list