Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!fxgrp!ljz From: ljz@fxgrp.UUCP (Lloyd Zusman) Newsgroups: comp.lang.c Subject: Re: Question on structures Message-ID: <150@fxgrp.UUCP> Date: Sun, 8-Nov-87 14:24:27 EST Article-I.D.: fxgrp.150 Posted: Sun Nov 8 14:24:27 1987 Date-Received: Tue, 10-Nov-87 04:06:30 EST References: <1025@phoenix.Princeton.EDU> <3022@bunker.UUCP> Reply-To: ljz@fxgrp.UUCP (Lloyd Zusman) Followup-To: <3022@bunker.UUCP> garys@bunker.UUCP (Gary M. Samuelson) Organization: Master Byte Software Lines: 66 Keywords: Sturctures records In article <3022@bunker.UUCP> garys@bunker.UUCP (Gary M. Samuelson) writes: >In article <1025@phoenix.Princeton.EDU> asjoshi@phoenix.UUCP (Amit S. Joshi) writes: >>I have gotten really tired of typing things like a.b.c.d etc to refer to >>elements of a structure ... There have been several suggestions, most of them involving the use of #define's for the higher-order structure elements (i.e., "#define foo a.b.c", thereby causing foo.d to refer to the a.b.c.d element). But I came up with a slightly different approach you might want to try ... #define WITH(Name, Type, Struct) { Type *Name = &(Struct); #define ENDWITH } Assume these three macros are defined. The following program fragment illustrates how they can be used ... struct baz { int a; int b; int c; }; struct bar { struct baz i; struct baz j; struct baz k; }; struct foo { struct bar x; struct bar y; struct bar z; }; struct foo p; struct foo q; struct foo r; . . . WITH (X, struct baz, p.z.i) X->a = 1; X->b = 2; X->c = 3; ENDWITH The above 5 lines translate to { struct baz *X = &(p.z.i); X->a = 1; X->b = 2; X->c = 3; } This is a variation of another method I saw here on the net. I do it this way so that you still have a WITH-like environment and the WITH section is in a separate block of code wherein you can define local variables, etc. If #define's could be nested within other #define's, you could get rid of one or two of the arguments to the WITH macro. Oh well. -- Lloyd Zusman, Master Byte Software, Los Gatos, California "We take things well in hand." ...!ames!fxgrp!ljz