dinmaic variable name

E

eyalc1978

Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?
 
I

Ian Collins

Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?
No, but it sounds like you should store the values in an array.
 
E

eyalc1978

Ian said:
No, but it sounds like you should store the values in an array.

Thanks
An array will be a good idea however I am getting these values from a
DB table into a struct with a lot of other fields
Is there a way to transform this struct? Or maybe some other way?
 
I

Ian Collins

Thanks
An array will be a good idea however I am getting these values from a
DB table into a struct with a lot of other fields
Is there a way to transform this struct? Or maybe some other way?
That depends on what the C wrapper to your database returns. Those I
have used return an array for each database row, so you should be able
to map to another array.

Is your code populating the struct?
 
E

eyalc1978

Ian said:
That depends on what the C wrapper to your database returns. Those I
have used return an array for each database row, so you should be able
to map to another array.

Is your code populating the struct?

The SQL that I am running on the DB to fetch the row, fetches it into
the struct. So the struct is being populated by the SQL.
 
I

Ian Collins

The SQL that I am running on the DB to fetch the row, fetches it into
the struct. So the struct is being populated by the SQL.
Please adjust your news reader so it does not quote signatures (the bit
under the --). This is normally a default setting.

If the DB (which one?) gives you a struct, you will have to do the
mapping yourself. At least you only have to code it once.

It might be worth checking to see if there is a function that returns an
array for each row.
 
D

dbtid

Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?

Well, you could simply create an array and then parse the variable names
that come from your database and use the result as an array index.

Say your name is "a20", and your array is int a[20];

/* use name+1 to skip the leading 'a' */
idx = strtol (name+1, NULL, 10);

if (idx >= 1 && idx <= 20)
result = a[idx-1];
 
M

Morris Dovey

(e-mail address removed) (in
(e-mail address removed)) said:

| Ian Collins wrote:
|| (e-mail address removed) wrote:
||| Hi
||| let's say I have 20 variables named a1-a20 which are all of the
||| same type (int)
||| I need to access each one of them and according to the value do
||| some operation.
||| Is there some way I can access them inside a loop and build
||| there name dinamically or do I have to access each one of them
||| seperatly?
|||
|| No, but it sounds like you should store the values in an array.
|
| An array will be a good idea however I am getting these values from
| a DB table into a struct with a lot of other fields
| Is there a way to transform this struct? Or maybe some other way?

Names of variables aren't visible in the executable. Once in the
structure, you may be able to use a table of pointers to access each
of your 20 int structure members in a loop.

struct
{ int a1;
int b1;
char c[10];
int a2;
:
int a20;
} db_rec;

int *pointer_table[] = { &db_rec.a1, &db_rec.a2, /*...*/
,&db_rec.a20 };

for (i=1; i<21; i++)
{ value = *pointer_table;
/* do some value-dependent opperation */
}
 
D

Default User

Ian Collins wrote:

Please adjust your news reader so it does not quote signatures (the
bit under the --). This is normally a default setting.


Not for Google. The capability doesn't exist.




Brian
 
D

Default User

santosh said:
But it's trivial enough that it can be manually done.

I would not disagree. It's important to remember that Google, while
it's fixed some things, still continues to handicap their users. Also
that many (not you of course) are newbies to newsgroups and can use
some help.

I changed my header display to include the user-agent line so I can
spot Google users:

User-Agent: G2/0.2

That way I can tailor my advice as needed.




Brian
 
M

Morris Dovey

Morris Dovey (in [email protected]) wrote:

some disgustingly incorrect C fragments (for statement corrected):

| struct
| { int a1;
| int b1;
| char c[10];
| int a2;
| :
| int a20;
| } db_rec;
|
| int *pointer_table[] = { &db_rec.a1, &db_rec.a2, /*...*/
| ,&db_rec.a20 };
|
for (i=0; i<20; i++)
| { value = *pointer_table;
| /* do some value-dependent opperation */
| }
 
W

websnarf

The SQL that I am running on the DB to fetch the row, fetches it into
the struct. So the struct is being populated by the SQL.

The original suggestion is still correct. You should do the following:

type * arr[21];

arr[1] = &sqlres.a1;
arr[2] = &sqlres.a2;
...
arr[20] = &sqlres.a20;

Then (*arr) will be a pointer alias to the ith variable. Keep in
mind that writing to these are unlikely to change your actual database
values, of course.
 
B

Barry Schwarz

On 4 Jul 2006 21:20:53 -0700, (e-mail address removed) wrote:

snip
The original suggestion is still correct. You should do the following:

type * arr[21];

arr[1] = &sqlres.a1;
arr[2] = &sqlres.a2;
...
arr[20] = &sqlres.a20;

Then (*arr) will be a pointer alias to the ith variable. Keep in
mind that writing to these are unlikely to change your actual database
values, of course.


arr is a pointer to the ith variable. *arr is the ith variable
itself. I don't know what you intended the word alias to convey.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top