struct accessing

David Riches dsr at stl.stc.co.uk
Thu Jun 22 22:03:01 AEST 1989


I have a problem here which I'd like to get round if possible.
Say I have a structure like :-

struct fred
  {
   int tom;
   int dick;
   int harry;
  }

To look at any particular field I would say; fred.tom or fred.dick etc.

Now, I have a variable which tells me the name of the field in fred
which I would like to look at, e.g. field_name.  So if field_name
holds the name dick I want to look at fred.dick and so on.

What I'm doing at the moment is using a case statement to interrogate
field_name and to switch to the appropriate statement which lets me
look at the field I want.

This gets messy when the struct gets big. Is there a more subtle way
of doing this?  For instance, 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.".

Does anyone have an elegant solution to this?

   Dave Riches
   PSS:    dsr at stl.stc.co.uk
   ARPA:   dsr%stl.stc.co.uk at earn-relay.ac.uk
   Smail:  Software Design Centre, (Dept. 103, T2 West), 
	   STC Technology Ltd., London Road,
	   Harlow, Essex. CM17 9NA.  England
   Phone:  +44 (0)279-29531 x2496



More information about the Comp.lang.c mailing list