getting variables in order

Allen Williams anw@csunv.com
Sat Sep 27 15:51:00 GMT 2003


You could also put them in a struct:

struct vars
{
char variables[17];
char *a;
char *b;
int  *c;
char *d;
char *e;
int  *f;
char *g;
} varstruct, *pvarstruct;

Now, you instantiate the structure and provide your users a pvarstruct to
access it.  My personal opinion is that this is the more appropriate way,
since this one of the things structures were meant for- it's more in line
with the form of the language.  Note that on some OS's you might have to
tell the compiler to pack the structure- using something like #pragma PACK.

In spite of the language support, these sorts of things aren't the most
portable code ever written.  Depending on your application, I'd be much more
inclined to use C++ and provide your users with access functions to the
variables they need.  Your mileage may vary!

----------
anw


> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
> Behalf Of Fabiano Ramos
> Sent: Saturday, September 27, 2003 8:38 AM
> To: gcc-help@gcc.gnu.org
> Subject: Re: getting variables in order
>
>
> Used an array instead. I do not know what is your machine, so you should
> use sizeof o ensure portability, but I'll assume 1 byte chars and 4 byte
> integers:
>
> char variables[17];
> char *a = &variables[0];
> char *b = &variables[1];
> int  *c = &variables[2];
> char *d = &variables[6];
> char *e = &variables[7];
> int  *f = &variables[12];
> char *g = &variables[16];
>
> Fabiano
>
> On Sat, 2003-09-27 at 03:14, Mike Johnson wrote:
> > I'd like to define a list of variables as so:
> >
> > char	a;
> > char	b;
> > int	c;
> > char	d;
> > char	e[5];
> > int	f;
> > char	g;
> >
> > and have them packed and in order in the map file.
> >
> > when I do this they are defined in some random order in the map
> > file.
> >
> > This is so I can access them as a pointer offset from a to allow
> > a user to query the state of the vars.
> >
> > I'm using the AVR version of GCC.
> >
> > Thanx for any ideas.
> >
> > Mike Johnson
> >
> >
> --
> Fabiano Ramos <fabramos@bol.com.br>



More information about the Gcc-help mailing list