on the fringe of C syntax/semantics

Troy Rollo troy at mr_plod.cbme.unsw.oz
Thu Oct 5 14:30:36 AEST 1989


>From article <80100001 at bonzo>, by roy at bonzo.sts.COM:

roy> cake, but how about a good, general declaration for a function?
roy> '(int *())' and '(int ())' were two tries at declaring a general function
roy> that returns an 'int', but they didn't work.  Are you stuck with
roy> something like '(int (*foo)())', where 'foo' is a particular function

Strictly, there's no legal way of doing this..... Hang on, yes there is:

typedef int (*function)();

Then when you're putting it in the code, user (function) as your type cast.

This isn't really on the edge of the language specs.... although I ran into
a question last night which was... somebody wanted to define a pair of
structures which were initialised with pointers to eachother. Fine, except
that one hasn't been defined - no address because no space is allocated, and
the compiler doesn't have the faintest idea what you're on about until later,
when you declare the second structure. The solution was to effect a forward
declaration by using the extern keyword. This causes the problem to be passed
off to the linker, which resolves the external reference - from the same file!

struct a_struct {
	void *next;
	int value;
};

struct b_struct {
	struct a_struct *next;
	int value;
};

extern struct b_struct struc2;
struct a_struct struc1 = { &struc2, 0 };
struct b_struct struc2 = { &struc1, 0 };
___________________________________________________________
troy at mr_plod.cbme.unsw.oz.au	Make our greenies useful!
The Resident Fascist		Put them in the army!



More information about the Comp.lang.c mailing list