Article View: gnu.gcc.help
Article #42631Re: Offsets in structures
From: Terry O'Brien
Date: Fri, 17 Nov 1995 00:00
Date: Fri, 17 Nov 1995 00:00
42 lines
1563 bytes
1563 bytes
Ian Dall (dall@hfrd015.dsto.gov.au) wrote:
: I have a problem where I wish to construct a table to access structure
: members. Eg:
: struct state {
: int dummy0;
: int r0, r2;
: int dummy1;
: int r1, r3;
: };
: int read_reg(struct state *t, int regno)
: {
: #define offset(n) (char *) &t->r ## n - (char *) &t->r0
: static const reg_offsets[] = { offset(0), offset(1), offset(2),
: offset(3)};
: #undef offset
: return *(int *)((char *) &t->r0 + reg_offsets[regno]);
: }
[ Question about gcc's rules on evaluating constants deleted ]
: Is there a better way of doing what I want? The struct state is not
: mutable. I'd prefer not to have a stack of hand calculated constants
: around.
Is there some reason why you don't use:
int read_reg(struct state* t, int regno)
{
switch(regno)
{
case 0: return t->r0;
case 1: return t->r1;
case 2: return t->r2;
case 3: return t->r3;
default: return 666; /* or whatever you want to do
given a duff regno! */
}
}
Happy Halloween
Technical Details
Message-ID:
<DI6t5r.3Jn@dsbc.icl.co.uk>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<488lt6$qg5@fang.dsto.defence.gov.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,45bd1c7bd76217 X-Google-Attributes: gid11318c,public From: tob@oasis.icl.co.uk (Terry O'Brien) Subject: Re: Offsets in structures Date: 1995/11/17 Message-ID: <DI6t5r.3Jn@dsbc.icl.co.uk> X-Deja-AN: 119586897 sender: news@dsbc.icl.co.uk references: <488lt6$qg5@fang.dsto.defence.gov.au> organization: ICL, Bracknell, UK. newsgroups: gnu.gcc.help