address of structure == address of first member? (long)

Wayne A. Throop throopw at xyzzy.UUCP
Thu Dec 1 05:03:23 AEST 1988


>,>>> gwyn at smoke.BRL.MIL (Doug Gwyn )
>>,>>>> carlp at 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      <the-known-world>!mcnc!rti!xyzzy!throopw



More information about the Comp.lang.c mailing list