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

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Nov 22 19:29:51 AEST 1988


In article <2172 at iscuva.ISCS.COM> carlp at iscuva.ISCS.COM (Carl Paukstis) writes:
>What other portability problems will I run into with this code?
1.  Use of prototypes constrains you to a modern compiler.
2.  You implicitly declare exit() as returning int, which is incorrect.
3.  sizeof results in a size_t but mybsearch() wants an int.  Although the
    type is automatically converted in the scope of the prototype, if you
    tried to use a really large struct the size might not fit.
4.  You really ought to #include <string.h> to declare strcmp().

>86	        code = strcmp (key, *(char **)((char *)table + (m * size)));
>Line 86 bothers me.  Is it permissible to cast a void* into a char* and do
>arithmetic on it like this?  Line 89 would have the same problem (if any).

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.

>On machines (environments?) with non-byte-sized chars, is there a better
>way to do the necessary arithmetic?  Or what else have I missed?

That seems about as good as any, if you don't want to tailor individual
versions to specific systems.



More information about the Comp.lang.c mailing list