Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
dinmaic variable name
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Morris Dovey, post: 2472668"] [email]eyalc1978@gmail.com[/email] (in [email]1151916189.967014.200880@a14g2000cwb.googlegroups.com[/email]) said: | Ian Collins wrote: || [email]eyalc1978@gmail.com[/email] 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 */ }[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
dinmaic variable name
Top