Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!ukc!stl!dsr From: dsr@stl.stc.co.uk (David Riches) Newsgroups: comp.lang.c Subject: struct accessing Message-ID: <1545@stl.stc.co.uk> Date: 22 Jun 89 12:03:01 GMT Sender: news@stl.stc.co.uk Reply-To: dsr@stl.stc.co.uk (David Riches) Organization: STC Technology Limited, London Road, Harlow, Essex, UK Lines: 38 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@stl.stc.co.uk ARPA: dsr%stl.stc.co.uk@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