Amiga-C (83/157)

From:Allan Odgaard
Date:18 Sep 2001 at 01:01:29
Subject:Re: [amiga-c] Re: Pointers in tables

On Mon, 17 Sep 2001, Lee Atkins wrote:

> > [...] ???
> Yes this is exactly what I need :)

Okay, personally I would probably do something like have all values in
an array and then simply index this array. E.g.

enum { Money = 0, Currency, ..., NumberOfValues };
struct MyStruct { int Values[NumberOfValues]; };

This allows you to write like this:

struct MyStruct v1;
v1.Values[Money] = 100;

You can have a function which translate a string to the proper index (if
you need to work with strings) and for C++ you can even overload the
[]-operator of your structure. You may also want to make the enumeration
loccal for the structure...

A solution closer to your original code would be to use the
offsetof()-macro. In your table you store the offset of the field, e.g.:

struct { STRPTR str; ULONG offset; } table[] =
{
{ "money", offsetof(MyStruct, money) };
...
};

VOID SetValue (STRPTR name, ULONG val, struct MyStruct *s)
{
for(int i = 0; i < ARRAYSIZE(table); i++)
{
if(!strcmp(name, table[i].str))
{
*(ULONG *)(((UBYTE *)s)+table[i].offset) = val;
break;
}
}
}

Of course this latter solution is not very nice because it uses typecasts
to access the member field, so I would strongly urge you to use something
closer to my first proposal ;-)



http://www.diku.dk/students/duff/

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Secure your servers with 128-bit SSL encryption! Grab your copy of VeriSign's FREE Guide, "Securing Your Web
site for Business" and learn all about serious security. Get it Now!
http://us.click.yahoo.com/r0k.gC/oT7CAA/yigFAA/dpFolB/TM
---------------------------------------------------------------------~->

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/