Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!ukma!uflorida!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: address of structure == address of first member? Message-ID: <14725@mimsy.UUCP> Date: 26 Nov 88 23:35:51 GMT References: <2172@iscuva.ISCS.COM> <8954@smoke.BRL.MIL> <2176@iscuva.ISCS.COM> <8976@smoke.BRL.MIL> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 49 [Amazing that no one else has jumped on this yet.] >In article <2176@iscuva.ISCS.COM> carlp@iscuva.ISCS.COM (Carl Paukstis) writes: >>86 code = strcmp (key, *(char **)((char *)table + (m * size))); [much edited] In article <8976@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn) suggests: >You already had a (char *). You then cast it to (char **), and then >dereferenced that using the * operator. There was no need for these >extra steps when the original (char *) was just what you needed. But the original `char *' is not what he needs, and this does not compile into an effective no-op (as you suggested in <8954@smoke.brl.mil>). If we write: char *p; p = (char *)table + (m * size); we have p pointing at (not `to': I am reserving that for a pointer of the appropriate type) an object of `struct '. This structure is defined as: struct { char *name; ... }; To be completely type-correct, we should convert `p' into a pointer to the correct structure type-name, and follow that to its `name' field. The problem, of course, is that in this general-purpose search routine, we have no idea *which* structure `p' is standing for [to end the sentence a preposition with]. There are two alternatives: define a structure that contains only a name; or cheat. struct name { char *name; }; result = strcmp(key, ((struct name *)p)->name); or result = strcmp(key, *(char **)p); The former is in some sense superiour, particularly under K&R rules, where any pair of structures that have an initial common set of names and types are, in that common part, compatible. (This rule is left over from the days before unions, and does not appear in the dpANS.) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris