Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!metaware!fjb From: fjb@metaware.metaware.com (Fred Bourgeois) Newsgroups: comp.lang.c Subject: Re: trouble with structures and callbacks Summary: structs are allowed as arguments in ANSI C Keywords: struct, pointer, argument Message-ID: <662@metaware.metaware.com> Date: 28 Nov 90 01:04:48 GMT References: <2450@kiwi.mpr.ca> <13837@june.cs.washington.edu> <1990Nov23.231941.14801@thyme.jpl.nasa.gov> <13840@june.cs.washington.edu> Reply-To: fjb@metaware.UUCP (Fred Bourgeois) Organization: MetaWare Inc., Santa Cruz, CA Lines: 42 Expires: Sender: Followup-To: Distribution: In article <13840@june.cs.washington.edu> slh@wolf.cs.washington.edu (Scott Heyano) writes: >In article <1990Nov23.231941.14801@thyme.jpl.nasa.gov> kaleb@thyme.jpl.nasa.gov (Kaleb Keithley ) writes: >||In article <2450@kiwi.mpr.ca> baker@mprgate.mpr.ca (Sue Baker) writes: >|| DAMNIT, you can only pass the POINTER TO THE STRUCTURE, >|| NOT the actual structure. >|| So only the first 32-bits are passed, in this case >|No, I don't think so. K&R1 says you can. Just because you might >|not ordinarily want to. Just because in this case it is definitely >|wrong. Doesn't make it *always* wrong. I seem to have missed the original article, but ANSI C does allow structs (not just struct *s) as arguments to functions. If your compiler doesn't handle this, its broken. If curious, try the code below, which should demonstrate passing (and returning) structs from functions. ----- #include typedef struct{float x;short a;long b;double y;char c;}Bar; static Bar foo(Bar z) { Bar q = { 2.1828, 256, 2147483647, 3.1415926535, 'z' }; return z = q; } static void foobar(Bar none) { printf("\tfloat x: %#f\n", none.x); printf("\tshort a: %d\n", none.a); printf("\tlong b: %ld\n", none.b); printf("\tdouble y: %#e\n", none.y); printf("\tchar c: %c\n", none.c); } main() { Bar xyzzy = { 0.0, 0, 0, 0.0, 'a' }, zzyzx; puts("before calling foo, xyzzy struct contains:"); foobar(xyzzy); zzyzx = foo(xyzzy); puts("\nafter calling foo, xyzzy struct contains:"); foobar(xyzzy); puts("\nAnd zzyzx struct contains:"); foobar(zzyzx); } ----- --- Fred Bourgeois, MetaWare Inc., 2161 Delaware Avenue, Santa Cruz, CA 95060-2806 fjb@metaware.com ...!uunet!metaware!fjb Colorless Green Ideas Sleep Furiously, and so do I.