trouble with structures and callbacks

Fred Bourgeois fjb at metaware.metaware.com
Wed Nov 28 12:04:48 AEST 1990


In article <13840 at june.cs.washington.edu> slh at wolf.cs.washington.edu (Scott Heyano) writes:
>In article <1990Nov23.231941.14801 at thyme.jpl.nasa.gov> kaleb at thyme.jpl.nasa.gov (Kaleb Keithley        ) writes:
>||In article <2450 at kiwi.mpr.ca> baker at 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 <stdio.h>
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 at metaware.com                                        ...!uunet!metaware!fjb
	     Colorless Green Ideas Sleep Furiously, and so do I.



More information about the Comp.lang.c mailing list