C question!
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 8 Aug 2008 17:04:01 -0700
Hello,
If I have a variable (lest call it "x") containing a number between 1 and 10
where this variable is to contain the numerical value corresponding
to a member of a structure, how can I access the member? For sample
purposes, right now I am using a structure.
For example:
============================================
struct msgs{
int MSG1;
int MSG2;
int MSG3;
int MSG4;
int MSG5;
int MSG6;
int MSG7;
int MSG8;
int MSG9;
int MSG10;
} *obj_msgs_x;
void main()
{
int x;
x = 4;
//... other code that will store 40 to the 4th member of the structure...
//see below explanations
}
=============================================
Now I know that to store 50 in the member called "MSG4" I can do:
obj_msgs_x->MSG4 = 50;
but what if I want to store the value of 50 at the member depending on the
value of "x" which contsains the nth member item of the structure?
I have to do something like this:
============================================
switch(x)
{
case 1:
obj_msgs->MSG1 = 28;
break;
case 2:
obj_msgs->MSG2 = 88;
break;
case 3:
obj_msgs->MSG3 = 9;
break;
case 4:
obj_msgs->MSG3 = 50;
break;
..... and so forth all the way til 10...
}
============================================
The above method can be very long and tedious if x can hold values from say
from 1 to 50 !!!!!
In summary, I would simply want to access *the* member item of the structure
using "x" by some other way if possible, using pointer arithmetic perhaps or
something like that.
Sorry if this is an ignorant question but all suggestions are appreciated
anyways!
P.S. Instead of a structure, I know I can use a simple array, or do an array
of strucutures but the all my special data is currently collected in simple
structures like
the one above and I don't want to break the trend right now!
--
Best regards
Robert
.
- Follow-Ups:
- Re: C question!
- From: Igor Tandetnik
- Re: C question!
- From: Mark Salsbery [MVP]
- Re: C question!
- Prev by Date: Re: Black border in CTreeCtrl icons
- Next by Date: achat zocor belgique au rabais en ligne sans prescription Achat achat zocor Pro achat zocor suisse soft sans prescription generique medicaments en France acheter zocor suisse pharmacie
- Previous by thread: weird results at run time
- Next by thread: Re: C question!
- Index(es):
Relevant Pages
|