1

I can read out all the variable values of a generic C struct (assuming all same variable types) using

struct whichstruct{
  float firstVar;
  float ...
  ...
};
whichstruct whichStruct;

void printParams(structType *whichStruct) {
  // Print out all values of a struct.
  float *startVar = &(whichStruct->firstVar);
  int numElements = sizeof(*whichStruct) / sizeof(startVar);
  for (int i = 0; i < numElements; ++i)
    printf("%d: %f\r\n", i, startVar[i]);
}

How can I store values equivalently, if I want to store to say member variable #10? This following does not work at the whichStruct[i] line, obviously, since it's not an array. But you get the idea...

void setParams(structType *whichStruct, const int whichVar, const float val) {
  // whichVar is the struct's member variable to access (2nd, 3rd, etc)
  float *startVar = &(whichStruct->firstVar);
  int numElements = sizeof(*whichStruct) / sizeof(startVar);
  int index = sizeof(startVar) * whichVar; // How many bytes into the struct?
  whichset[index] = val; // <-- trying to poke the value at the struct's correct byte address
  printf("#%d = %f\r\n", whichVar, whichStruct[index]);
}

How can I store directly by indexing into the struct? I want to write to the address directly, it seems, using something like

int* address = whichStruct + index; 
*address = val;
7
  • 2
    Sounds like you want an array rather than a struct. Or perhaps a struct containing an array?
    – John3136
    May 29, 2015 at 2:14
  • 1
    If you want a struct and the features of an array, maybe you could use a union.
    – Dko
    May 29, 2015 at 2:16
  • @Dko what do you mean? That's not what unions are for, how would you use it for that, it sounds interesting! May 29, 2015 at 2:19
  • 1
    Wouldn't your printParams example work only if every member of the struct was known to be of type float? Seems like having a struct of different sized types would break the whole concept.
    – paisanco
    May 29, 2015 at 2:21
  • 1
    startVar[whichVar] = val; should work. But the whole idea of treating your struct as an array is a bad one. You should rethink your data types.
    – JS1
    May 29, 2015 at 2:23

3 Answers 3

3

You can use the offsetof macro to find the byte offset into the struct. Then you just do some pointer arithmetic to access the field.

*((float *) (((void *)&whichStruct) + offsetof(whichstruct, firstVar))) = val;
3
  • You could cache these offsets in an index array in the struct and access with double indirection *(float*)&((char*)sp)[sp->index[i]]. May 29, 2015 at 6:20
  • offsetof(whichstruct, firstVar) is required to be zero. The standard is quite explicit about it. May 29, 2015 at 9:40
  • You're correct, I was just using that as an example. The OP did not give the names of later fields.
    – b4hand
    May 29, 2015 at 15:42
2

Since all variables are of the same type, the fields are going to be stored next to each other, with no gaps between them. Therefore, you can index into your struct like this:

void setParams(structType *whichStruct, const int whichVar, const float val) {
    float *startVar = &(whichStruct->firstVar);
    startVar[whichVar] = val;
    printf("#%d = %f\r\n", whichVar, whichStruct[index]);
}

Another alternative would be to replace the individual fields of your struct with an array of equivalent size, and provide a #define-d constant for each field name. This way your code would reflect the same meaning, and provide a way to access array elements in a natural way.

5
  • I don't think that just because they are the same type you can assume there is no padding between fields in a struct. In particular I could see a compiler choosing to include padding between types that are potentially smaller than word size like short or char. This would be a space versus access time tradeoff that a compiler could make depending on the optimization settings.
    – b4hand
    May 29, 2015 at 2:40
  • @b4hand According to the standard, padding is inserted only when (1) a structure member is followed by a member with a larger alignment requirement or (2) at the end of the structure. May 29, 2015 at 2:45
  • This disagrees with you. It says that an implementation is allowed to require arbitrarily strict alignment rules for struct fields.
    – b4hand
    May 29, 2015 at 3:28
  • From C99 §6.7.2.1: Each non-bit-field member of a structure or union object is aligned in an implementation-defined manner appropriate to its type.
    – b4hand
    May 29, 2015 at 3:37
  • @b4hand Alignment can change between fields of different types, causing the compiler to insert padding. However, when types are the same, the alignment stays the same, hence there will be no padding. May 29, 2015 at 9:38
0

A more elaborate version of dasblinkenlight's alternate suggestion is one of my favorite things to do. You can use an X-Macro to describe the table with symbolic names for each entry. The X-Macro lets you peel-off the symbols into an enum and then use the other data to initialize an array. Edits to the table maintain the symbolic association (unless, of course, you screw it up somehow, always a possibility).

#define FLOATTAB(_) \
    _(FirstVar, 1.0) \
    _(SecondVar, 2.0) \
    /* end FLOATTAB */
#define FLOATTAB_NAME(a,b) a,
enum { FLOATTAB(FLOATTAB_NAME) };
#define FLOATTAB_DATA(a,b) b,
float ftab[] = { FLOATTAB(FLOATTAB_DATA) };

Then you can access a data member with the symbolic name.

ftab[FirstVar];

which is almost as clear as fstruct.FirstVar;, but puts everything in an array, and FirstVar is now an index, a small integer which can be stored in a variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.