🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2016

Wanted: struct's address from member's address please.

#2016
From: Ronald S H Khoo
Date: Fri, 23 Nov 1990 09:34
27 lines
1194 bytes
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@robobar.co.uk +44 81 991 1142 (O) +44 71 229 7741 (H)
Technical Details
Message-ID: <1990Nov23.093458.24957@robobar.co.uk>
Path: arc1-ca.pugleaf.net!utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!rutgers!sun-barr!cs.utexas.edu!samsung!dali.cs.montana.edu!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!stl!robobar!ronald

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!rutgers!sun-barr!cs.utexas.edu!samsung!dali.cs.montana.edu!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!stl!robobar!ronald
From: ronald@robobar.co.uk (Ronald S H Khoo)
Newsgroups: comp.std.c
Subject: Wanted: struct's address from member's address please.
Message-ID: <1990Nov23.093458.24957@robobar.co.uk>
Date: 23 Nov 90 09:34:58 GMT
Organization: Robobar Ltd., Perivale, Middx., ENGLAND.
Lines: 27