Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!uflorida!gatech!mcnc!rti!xyzzy!throopw From: throopw@xyzzy.UUCP (Wayne A. Throop) Newsgroups: comp.lang.c Subject: Re: address of structure == address of first member? (long) Message-ID: <2110@xyzzy.UUCP> Date: 30 Nov 88 19:03:23 GMT References: <2172@iscuva.ISCS.COM> <8954@smoke.BRL.MIL> <2176@iscuva.ISCS.COM> <8976@smoke.BRL.MIL> Organization: Data General, RTP NC. Lines: 39 >,>>> gwyn@smoke.BRL.MIL (Doug Gwyn ) >>,>>>> carlp@iscuva.ISCS.COM (Carl Paukstis) >>>>86 code = strcmp (key, *(char **)((char *)table + (m * size))); >>>Line 86 is okay, but you really don't need to cast to a (char**) then >>>dereference to get the (char*) key. That's extra work that amounts to >>>a no-op. >> Huh? [... It sure seems necessary to me ...] > 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. > (strcmp takes (char *) arguments.) Oooooh, Doug, you ought to know better. The stuff you count as "extra work that amounts to a no-op" is not a no-op at all, but the dereference of a crucial pointer. Look at those structure definitions again. The first elements of those structures are not (char []), but (char *). What is gotten by ((char *)table + (m*size)) is a pointer to this pointer (but of the wrong type (1)). The fact that this pointer to the desired pointer happens to be typed in the same way as the desired pointer itself is just a coincidence, required for reasons of doing byte-granular pointer arithmetic in C. Hence, the cast and dereference is in NO WAY a no-op, and is correct and necessary for Carl's code to work properly. The alternate (and more portable) way to do what Carl is trying to do is to pass in routines to do subscripting and comparison. But I suspect the porting problems of Carl's code as it is are miniscule. (1) To pick nits, that's a pointer to a structure containing the desred pointer as its first element, which is what the subject of this discussion thread is all about. -- In Loglan, time flies like an arrow precisely the way fruit flies like a banana; there isn't any other way of saying it. --- D.E. Cortesi in Dr Dobbs Journal -- Wayne Throop!mcnc!rti!xyzzy!throopw