By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,107 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,107 IT Pros & Developers. It's quick & easy.

dinmaic variable name

P: n/a
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?

Jul 3 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
ey*******@gmail.com 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.

--
Ian Collins.
Jul 3 '06 #2

P: n/a

Ian Collins wrote:
ey*******@gmail.com 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.

--
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?

Jul 3 '06 #3

P: n/a
ey*******@gmail.com wrote:
Ian Collins wrote:
>>ey*******@gmail.com 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.

--
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?

--
Ian Collins.
Jul 3 '06 #4

P: n/a

Ian Collins wrote:
ey*******@gmail.com wrote:
Ian Collins wrote:
>ey*******@gmail.com 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.

--
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?

--
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.

Jul 3 '06 #5

P: n/a

Ian Collins 写道:
ey*******@gmail.com 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.

--
Ian Collins.
Jul 3 '06 #6

P: n/a
ey*******@gmail.com wrote:
Ian Collins wrote:
>>ey*******@gmail.com wrote:
>>>Ian Collins wrote:
ey*******@gmail.com 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.
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?

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.

--
Ian Collins.
Jul 3 '06 #7

P: n/a
ey*******@gmail.com 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?
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];

Jul 3 '06 #8

P: n/a
ey*******@gmail.com (in
11**********************@a14g2000cwb.googlegroups. com) said:

| Ian Collins wrote:
|| ey*******@gmail.com 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[i];
/* do some value-dependent opperation */
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 3 '06 #9

P: n/a
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
Jul 3 '06 #10

P: n/a
Default User wrote:
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.
But it's trivial enough that it can be manually done.

Jul 3 '06 #11

P: n/a
santosh wrote:
Default User wrote:
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.

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

Jul 3 '06 #12

P: n/a
Morris Dovey (in 0e**************@news.uswest.net) 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[i];
| /* do some value-dependent opperation */
| }

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 4 '06 #13

P: n/a
ey*******@gmail.com wrote:
Ian Collins wrote:
ey*******@gmail.com wrote:
Ian Collins wrote:
>>ey*******@gmail.com 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.
>>
>>--
>>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?

--
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.
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[i]) 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.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 5 '06 #14

P: n/a
On 4 Jul 2006 21:20:53 -0700, we******@gmail.com 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[i]) 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[i] is a pointer to the ith variable. *arr[i] is the ith variable
itself. I don't know what you intended the word alias to convey.
Remove del for email
Jul 6 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.