Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!rutgers!mtune!codas!cpsc6a!rtech!wrs!dg From: dg@wrs.UUCP (David Goodenough) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <220@wrs.UUCP> Date: Mon, 6-Jul-87 19:36:52 EDT Article-I.D.: wrs.220 Posted: Mon Jul 6 19:36:52 1987 Date-Received: Sat, 11-Jul-87 00:36:11 EDT References: <8286@ut-sally.UUCP> <7001@alice.UUCP> <364@sol.ARPA> <1158@copper.TEK.COM> <1213@carthage.swatsun.UUCP> Reply-To: dg@wrs.UUCP (David Goodenough) Organization: Wind River Systems, Emeryville, CA Lines: 68 In article <1213@carthage.swatsun.UUCP> rice@swatsun (Dan Rice) writes: > Here's a simple question for you. Suppose I have defined structures >containing other structures, i.e., > >typedef struct { > float x, y, z; /* Coordinates of a vector in 3-space */ >} vector; > >typedef struct { > vector o; /* Center */ > float r; /* Radius */ >} sphere; > >sphere s1, *s2; > >Now, say I refer to s1.o.x and s2->o.y. Does the compiler convert this into >a simple address reference at compile time, or is work performed at runtime? It's done at compile time. >Should I define > vector center; > center = s2->o; >if I plan to use s2->o several times? Thanks for any help. You'd do better with vector *center; center = &(s2->o); and refer via center->. You will get two savings out of doing it this way: whenever you do a structure reference e.g. foo.bar the compiler internally has to convert it to (&foo)->bar i.e. it's more eficient to work with the address of a structure; and center = s2->o is a whole structure assignement (12 bytes worth of move on a 68K) whereas center = &(s2->o) is only a pointer assigment (i.e. 4 bytes). NOTE also that these two will have very different results if you start assigning back into center: with your method since center is a complete new copy of the structure center.x = 3.0 would not assign 3.0 into s2->o.x; whereas with pointer work center->x = 3.0 drops the value into s2->o.x - something to be aware of when you're deciding which way to go. -- dg@wrs.UUCP - David Goodenough +---+ | +-+-+ +-+-+ | +---+