[comp.lang.c] passing structures

andrew@motto.UUCP (Andrew Walduck) (10/11/90)

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@motto.UUCP | I wasn't aware that Ada was useful - Henry Spencer|

steve@taumet.com (Stephen Clamage) (10/11/90)

andrew@motto.UUCP (Andrew Walduck) writes:

|In the new ANSI standard, we can now pass (and return) a structure by
|value. Like so...(fragment follows)

|typedef struct complex { int real; int imag; } complex;
|complex add(complex, complex); /* function prototype for complex add */

|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:

|result = add(a,{5,8});

|But this isn't supported by ANSII! There's no way to pass a structure
|as a parameter!

It was never possible to create such an anonymous structure constant in
Classic C, and is still not possible in ANSI C.  You can however do
this easily in C++, where such things are fully supported.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

henry@zoo.toronto.edu (Henry Spencer) (10/11/90)

In article <241@motto.UUCP> andrew@motto.UUCP (Andrew Walduck) writes:
>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?

No prior art and limited need.  You can pass structures as parameters all
you want, but there is no way to compose a structure constant "on the fly".
You have to do something like this:

	{
		complex c58 = { 5, 8 };
		...
		result = add(a, c58);

There are various subtle problems with "structure constant" schemes; it is
not as simple as it looks, especially when unions enter the picture.  It's
not unsolvable, mind you, but it's not as trivial as you seem to think.
Given this, and the near-total lack of experience with it, X3J11 decided
not to include it in ANSI C.

>Any idea how I can suggest this to the committee?

Implement it in a compiler, have it used by yourself and others for a
few years, and propose it when ANSI C comes up for revision.  You can't
propose it now; nobody is listening.  The odds of proposing it successfully
at revision time go way up if you have an implementation and good
experience to report.

Actually, I believe there already are implementations that do this (in
various ways), so it may be just a matter of adding your voice -- when
the time comes -- to the folks saying "we've used this, it works".
-- 
Imagine life with OS/360 the standard  | Henry Spencer at U of Toronto Zoology
operating system.  Now think about X.  |  henry@zoo.toronto.edu   utzoo!henry

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (10/12/90)

In article <241@motto.UUCP>, andrew@motto.UUCP (Andrew Walduck) writes:
> typedef struct { int real, imag; } complex;

> complex add(complex, complex); /* function prototype for complex add */
... {
>   complex result, a, b;

> 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:

> result = add(a,{5,8});

> But this isn't supported by ANSII!
> Any idea how I can suggest this to the committee?

It's *way* too late.  The ANSI C standard is finished.
But there is nothing at all to stop you writing
	{ static complex k5_8 = {5,8};
	  result = add(a, k5_8);
	}
Or you could do it the Fortran way:
	complex CMPLX(int real, int imag)
	    {
		complex temp;
		temp.real = real, temp.imag = imag;
		return temp;
	    }
...
	result = add(a, CMPLX(5, 8));
I prefer this approach myself, because then you can change the order
(and number) of the fields in the struct without having to change the
rest of your program, as long as the interface of CMPLX is not changed.

With 'inline' functions (C++, some C compilers) this approach comes
pretty close to what you want anyway.
-- 
Fear most of all to be in error.	-- Kierkegaard, quoting Socrates.

cudcv@warwick.ac.uk (Rob McMahon) (10/13/90)

In article <241@motto.UUCP> andrew@motto.UUCP (Andrew Walduck) writes:
>complex add(complex, complex); /* function prototype for complex add */
>...
>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:
>...
>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?

It is do-able, You can do this in GCC, although you still need a cast:

  result = add(a,(complex){5,8});

So there is some prior art, but ...

>Any idea how I can suggest this to the committee?

... you're much too late to get it into ANSI-C this time round, so any use
will be completely non-portable.  Still, if it proves useful enough, and other
vendors take up the idea, it may make it in next time.

Rob
--
UUCP:   ...!mcsun!ukc!warwick!cudcv	PHONE:  +44 203 523037
JANET:  cudcv@uk.ac.warwick             INET:   cudcv@warwick.ac.uk
Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England

seanf@sco.COM (Sean Fagan) (10/14/90)

In article <1990Oct11.162737.11332@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
>In article <241@motto.UUCP> andrew@motto.UUCP (Andrew Walduck) writes:
>>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?

gcc already does this, although you need a typecast.  So there's your
prior-art.  I, however, have never used it (other than verifying that it
works, just now), and henry's suggestion works just as well.

-- 
-----------------+
Sean Eric Fagan  | "*Never* knock on Death's door:  ring the bell and 
seanf@sco.COM    |   run away!  Death hates that!"
uunet!sco!seanf  |     -- Dr. Mike Stratford (Matt Frewer, "Doctor, Doctor")
(408) 458-1422   | Any opinions expressed are my own, not my employers'.

cdm@gem-hy.Berkeley.EDU (Dale Cook) (10/15/90)

In article <241@motto.UUCP>, andrew@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@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.

eager@ringworld.Eng.Sun.COM (Michael J. Eager) (10/24/90)

In article <1990Oct13.150132.9257@warwick.ac.uk> cudcv@warwick.ac.uk (Rob McMahon) writes:
>In article <241@motto.UUCP> andrew@motto.UUCP (Andrew Walduck) writes:
>>complex add(complex, complex); /* function prototype for complex add */
>>...
>>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:
>>...
>>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?
>
>It is do-able, You can do this in GCC, although you still need a cast:
>
>  result = add(a,(complex){5,8});
>
>So there is some prior art, but ...
>
>>Any idea how I can suggest this to the committee?
>
>... you're much too late to get it into ANSI-C this time round, so any use
>will be completely non-portable.  Still, if it proves useful enough, and other
>vendors take up the idea, it may make it in next time.

Structure constants were suggested to the ANSI C committee by several
people, myself included.  There wasn't adequate support nor adequate 
prior art.  Perhaps it will be in the next revision.

-- Mike Eager
>