passing structures

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Fri Oct 12 11:24:05 AEST 1990


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



More information about the Comp.lang.c mailing list