struct accessing

Reinhard Foessmeier foessmei at lan.informatik.tu-muenchen.dbp.de
Mon Jun 26 21:23:43 AEST 1989


In article <1545 at stl.stc.co.uk> dsr at 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 at infovax.informatik.tu-muenchen.dbp.de |      voi che entrate!"
   [ { relay.cs.net | unido.uucp } ]             |      (Dante, Inferno)



More information about the Comp.lang.c mailing list