GOOD things about C - (nf)

ucbesvax!turner ucbesvax!turner
Sat Apr 2 03:11:59 AEST 1983


#N:ucbesvax:4800002:000:1394
ucbesvax!turner    Apr  1 14:36:00 1983

	One of the things I like about C is that I can do just about
    anything that I can in assembler.  Address arithmetic is a good
    example:

	struct foo {
		...
		int bar;
		...
		int quux;
		...
	};

	/*
	 * DIST - find distance between two addresses in something of the
	 *	same structure as pointer p.
	 */
	#define DIST(p, t1,t2) ((char *) &(p)->t2) - ((char *) &(p)->t1)

	#define NULL 0

	#define GAPSIZE DIST ((struct foo *) NULL, bar, quux)

	References to GAPSIZE do not presuppose the existence of anything
    of structure foo, and GAPSIZE compiles as a constant despite the
    apparently "illegal" reference through a NULL pointer.

	This is quite convenient when, for example, one has very large
    dynamically allocated structures with perhaps 1% of header info in
    each.  Certainly one doesn't want to prematurely declare some static
    thing of this size before it's known whether it will be used.
    Moreover, a manifest constant for header length would be difficult
    to maintain if the format of the header were often changed, and would
    not port in any case.  THIS scheme provides a means for maintaining
    it, assuming that one is careful about writing portable code
    in the first place.  (I.e., we should be careful how we address
    the "first" byte of an int or long.)

	Always Ready with a Gross Hack,
	    Michael Turner
	    ucbvax!esvax:turner





More information about the Comp.lang.c mailing list