Wanted: struct's address from member's address please.
Ronald S H Khoo
ronald at robobar.co.uk
Fri Nov 23 20:34:58 AEST 1990
Can someone give me a pointer to relevant bits of the Standard please ?
I'd like to know if
1) I can have a strictly conforming portable function that returns
the address of a structure, given the address of a member of
said structure,
2) Is this function the one I want ? (and is it strictly conforming?)
#include <stddef.h>
/* typedefs to define type1 and type2 */
struct foo {
type1 first_bit;
type2 second_bit;
};
struct foo *
func(type2 *arg)
{
return (struct foo *) ((char *)arg -
offsetof(struct foo, second_bit));
}
--
ronald at robobar.co.uk +44 81 991 1142 (O) +44 71 229 7741 (H)
More information about the Comp.std.c
mailing list