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