struct accessing

Jacob Gore jacob at gore.com
Mon Jun 26 02:43:28 AEST 1989


/ comp.lang.c / dsr at stl.stc.co.uk (David Riches) / Jun 22, 1989 /
struct fred
  {
   int tom;
   int dick;
   int harry;
  }
... in my dreams, I would like to have a statement which says :-

	person = fred.$field_name$

where $field_name$ means "take my contents and use that as the field
name.".
----------

Something that comes close:

struct fred
  {
   int tom;
   int dick;
   int harry;
  } fred_proto;
#define TOM	(&fred_proto.tom   - &fred_proto)
#define DICK	(&fred_proto.dick  - &fred_proto)
#define HARRY	(&fred_proto.harry - &fred_proto)
...
int field_name;
struct fred fred;
int *person;
...
person = &fred + field_name;


Or some variation of the above, like
#define FIELD(aStruct,offset)	(*(&aStruct + offset))
int person = FIELD(fred,TOM);

--
Jacob Gore	Jacob at Gore.Com		{oddjob,chinet,att}!nucsrl!gore!jacob



More information about the Comp.lang.c mailing list