Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!cs.utexas.edu!uunet!mcvax!unido!ztivax!tumuc!lan!foessmei From: foessmei@lan.informatik.tu-muenchen.dbp.de (Reinhard Foessmeier) Newsgroups: comp.lang.c Subject: Re: struct accessing Summary: structures vs. arrays Keywords: structures arrays Message-ID: <640@tuminfo1.lan.informatik.tu-muenchen.dbp.de> Date: 26 Jun 89 11:23:43 GMT References: <1545@stl.stc.co.uk> Sender: news@lan.informatik.tu-muenchen.dbp.de Reply-To: foessmei@lan.informatik.tu-muenchen.dbp.de (Reinhard Foessmeier) Organization: Inst. fuer Informatik, TU Muenchen, FR Germanujo Lines: 46 In article <1545@stl.stc.co.uk> dsr@stl.stc.co.uk (David Riches) writes: ..... >Say I have a structure like :- > >struct fred > { > int tom; > int dick; > int harry; > } > >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. > ..... > >Does anyone have an elegant solution to this? Since you want to process the ^Car vi volas procezi la erojn component names of your structure, de via strukturo, la sola racia the only reasonable way seems to vojo ^sajnas la uzo de vektoro be to use an array instead of a anstataw strukturo. Vi povas struct. You may #define symbolic #difini simbolajn nomojn por names for the indices, e.g. la indicoj, ekz-e #define TOM 0 #define DICK 1 #define HARRY 2 int fred[3]; Now you can access the components Nun vi povas aliri la erojn kiel as "fred[TOM]" &ct. Fiddling with fred[TOM] ktp. Procezumi la plenajn the full strings is probably a nomojn probable estas mal^sparo de waste of resources anyway. Only tempo kaj spaco. Nur kiam vi volas if you want to read a name from legi nomon de ekstera dosiero, vi an external file you have to convert devas konverti ^gin (ekz-e en it (in a switch statement, for instance), switch-instrukcio), sed but that is the place where conversion tio estas la ^gusta loko por kon- belongs. vertado. Sorry for my crude English; Those who know ILo may benefit from the right column. Reinhard F\"ossmeier, Technische Univ. M\"unchen | "Lasciate ogni speranza, foessmeier@infovax.informatik.tu-muenchen.dbp.de | voi che entrate!" [ { relay.cs.net | unido.uucp } ] | (Dante, Inferno)