Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union - How do you know which union parameter is in use?

Tags:

c

unions

I have the following piece of code:

struct sched_param {
    union {
        int sched_priority;
        struct rcost_sched_param rcost_params;
    };
};

I want to know which of the two parameters is "active". Is there a way for me to do that other than adding a int to the struct sched_param?

like image 258
matanc1 Avatar asked Jan 17 '26 19:01

matanc1


1 Answers

struct sched_param {
  int type;
  union {
       int sched_priority;
        struct rcost_sched_param rcost_params;
  };
}

you can add member named type,save the data which parameters is "active"

like image 80
sundq Avatar answered Jan 21 '26 07:01

sundq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!