C question!



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
.



Relevant Pages

  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)
  • Re: Adding large numbers in C
    ... one of the numbers - or perhaps the result - is too big to store in an int. ... you have bitstrings longer than 8 bits, simply use an array of unsigned ... Incidentally, the subtraction routine does similar juggling, so if M and N ...
    (comp.lang.c)
  • Re: Reading a Text file
    ... int main ... In any case, you said earlier you want to store the data in an array, ... don't store its result in a char. ...
    (comp.lang.c)
  • Re: array allocaton size
    ... > int x; ... > then I create an array of fred: ... The compiler needs to store somewhere how many elements are ... This information is needed at destruction time ...
    (comp.lang.cpp)