-1

I have a structure coloring with bit fields defined in C, and one such structure called color

struct coloring {
    unsigned int a : 3;
    unsigned int b : 3;
    unsigned int c : 3;
    unsigned int d : 3;
    unsigned int e : 3;
    unsigned int f : 3;
   ...
    unsigned int v : 3;
};
void main(){
struct coloring color;
}

I want to access and possibly edit a certain element, depending on rules extraneous to this specific question. During the running time, I figure out which element I want to ask and let char letter = the letter corresponding to the element I want. How do I get from the letter corresponding to the element I want to the value of that element of the structure?

I cannot use output=color.letter as the structure has no element named 'letter' rather an element of the field with the same name as the value of letter. I could use something like

if (letter=='a') {output = color.a}
if (letter=='b') {output = color.b}
...many lines...
if (letter=='v') {output=color.v}

But I would like a better way. I have looked for ways using pointers but I do not think they work since I am using bit fields. I appreciate any help!

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    You're storing a color as 22 separate 3-bit fields? RGB is three fields, RGBA is four. What sort of terrifying color format are you using here? Bitfields aside, a 22-field struct is nothing to be proud of to begin with. Are you sure you didn't mean to use an array here? – Silvio Mayolo Nov 04 '22 at 20:58
  • It sounds like you are looking for something like [`std::bitset`](https://en.cppreference.com/w/cpp/utility/bitset), where you can access individual bits by index. Combining three neighboring bits into an integer is left as an exercise for the reader. – Igor Tandetnik Nov 04 '22 at 20:59
  • 1
    `main` should be declared `int main(void)` or `int main(int argc, char *argv[])`, not `void main()`. – Eric Postpischil Nov 04 '22 at 21:01
  • You'll need a selector that's either a free-standing function or a member function. – Eljay Nov 04 '22 at 21:23
  • @SilvioMayolo I am using 7 colors to color the edges of a graph. I was hoping to compact the data I was storing for each separate edge coloring. Hope this makes more sense. – Excited and Confused Nov 05 '22 at 04:15
  • @EricPostpischil My apologies, thank you for letting me know, I will exercise your recommendation while tagging in the future. Thanks also for the help for the main part. – Excited and Confused Nov 05 '22 at 04:19
  • @IgorTandetnik Thanks for the suggestion, I will make sure to look into this! I really appreciate it (: – Excited and Confused Nov 05 '22 at 04:20

1 Answers1

1

C does not provide dynamic selection of bit-fields or structure members. Your practical choices are generally to write your own access routines to select a field, likely using a switch rather than a series of if statements, or to manipulate the bits using bit-shift operators such as <<, >>, &, and |.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312