Is is possible to dynamically call a variable inside a struct?

Before I ask my question let me lay out a bit of theoretical code which obviously dosn't work:

struct test {
  int A;
  int B;
};
char x = 'A';

void setup(){
  test tS;
  tS.A = 2;
  tS.B = 34;
  Serial.begin(19200);
  Serial.println(tS.A);
  Serial.println(tS.B);
  //The theoredical part below
  Serial.println(tS.x);
  
  //In this case I would want the serial to show 2, then 34, then 2 again.

}

void loop(){
  
}

Is it possible to get the value of tS.A or tS.B using the value contained in char x?

Are you saying that you want to refer to a structure member by a character representing it's name? No.

You could combine A nd B into a two element array, and have x as the subscript.

One way for a small amount of values to read is by a switch statement, say within in a function:

int codeValue(char x)
{
  switch (x)
  {
    case 'A':
      return 2;
    case 'B':
      return 34;
  }
  return 0; // default
}
...later on...
Serial.println( codeValue(x) );

However, another option (as AWOL mentioned) is an array - since you seem to be using single letters, and there are at most 256 single letter char values (ASCII coding), then an array of 256 values would give you an entry for any single char:

void setup()
{
  int test[256];
  memset(test,0,sizeof(test)); // clear test array
  test['A']=2;
  test['B']=34;
  Serial.begin(19200);
  Serial.println(test['A']);
  Serial.println(test['B']);
  //The theoretical part below
  int x='Z';
  Serial.println(test[x]);

Notice if you do this you can 1) only use single characters 2) they must be in single quotes 3) when you use a variable (like x) it CANNOT be in quotes. As well, for safety there are 256 entries in test, many of which will be wasted (a total of 256 int values, or 512 bytes). You have to decide if the convenience is worth the memory use (of course, you can reduce this if you use a function instead of an array, but that gets more complicated). And if you know what range you are using, you can shorten it - for example, if you know you can never enter a non-alphabetic character, you will never need the upper 128 items, so the array can be test[128];

Another option

struct test {
  int A;
  int B;
} x = {2,34};

char c = 'A';

void setup()
{
  Serial.begin(115200);
  Serial.println( c == 'A'?x.A:x.B );  // do a ternary  if in the printstatement (but I don't like it because of its readability ...
}

void loop(){}

[quote author=David Pankhurst link=topic=66304.msg486247#msg486247 date=1310457050] However, another option (as AWOL mentioned) is an array - since you seem to be using single letters, and there are at most 256 single letter char values (ASCII coding), then an array of 256 values would give you an entry for any single char: [/quote]

Watch that char 0x80 .. 0xff are negative. This tends to make them a little tricky as array subscripts. Also, ASCII defines 0x00 .. 0x7f (only 128 code points)

Thanks for your answers. The intent of my query had been to find out if I could assign a float value from a serial input into a struct, and so far it seems it's impossible. To be specific, I'm programming a G-Code interpreter and was hoping I could assign the X,Y, and Z coordinates with a single function rather than three separate functions. I would love any suggestions towards that end.

The intent of my query had been to find out if I could assign a float value from a serial input into a struct, and so far it seems it’s impossible

Not at all impossible - the float is simply a four byte wide variable.
Read in four bytes and move to the appropriate place in the variable.
Be aware of possible endian differences between the two ends of your serial link.

AWOL: Read in four bytes and move to the appropriate place in the variable.

Erm. I'm not even sure what to look up to find out how to do that. Could you give me a lead to follow up?

The intent of my query had been to find out if I could assign a float value from a serial input into a struct, and so far it seems it’s impossible

unions !!

union magic 
{
   float f;
   byte b[4];
} x;

struct y
{
  float pi;
} y;

and then somewhere 

if (Serial.available >=4)
  for (int i=0; i< 4; i++)
    x.b[i] = Serial.Read();

y.pi = x.f;
[/]

I'm delighted that you pointed out unions, as they answer a separate musing that I had, however they don't help me in this particular case.

I want to know if i can assign x.f to y.pi inside a function by providing the function with 'pi'.

For example if struct y had both the float pi and an additional float root2, I would want to pass the value in x.f into y.pi by calling a hypothetical function "assignment".

void assignment(char yvalue){
  y."yvalue" = x.f;
}

That way, I could do this:

void assignment(char yValue){
  if (hasInstruction('yValue')){
    y.'yValue' = getInstruction('yValue');
  }
}

For reference sake, hasInstruction checks a multidimensional array for an entry that begins with 'yValue' and getInstruction provides the value following the 'yValue'. The array is filled with g-code commands sent via serial and parsed at spaces. I hope this jumbled mess of concepts make sense...

[quote author=Arch Hegemon link=topic=66304.msg487064#msg487064 date=1310543477]

void assignment(char yvalue){
  y."yvalue" = x.f;
}

[/quote]

The identifiers you assign to the structure members are cast in stone at compile time. The compiler generates instructions that are based on the memory offsets of the structure members and the final code does not contain any information at all regarding the original identifiers you used. It is impossible for C to directly handle something similar to what you mention here.

Something like this is possible:

enum day {
    saturday,
    sunday,
    monday
};

struct
{
   float saturday,
          sunday,
          monday;
} y;

void assignment(enum day yvalue){
  switch (yvalue) {
     case saturday: y.saturday = x.f; break;
     case  sunday: y.sunday = x.f; break;
     case  monday: y.monday = x.f; break;
  }
}

If the indexes must be actual strings, then you will need to implement a lookup table that allows you to look up the string name and determine the field.

I hope this jumbled mess of concepts make sense...

Well... No. It really doesn't.

I fail to see what this:

void assignment(char yvalue){
  y."yvalue" = x.f;
}

has anything to do with being able to do this:

The intent of my query had been to find out if I could assign a float value from a serial input into a struct, and so far it seems it's impossible.

The latter is entirely possible, but has nothing to do with the former, which is pretty much impossible.

So what exactly are you trying to accomplish with the former?