문제

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?

도움이 되었습니까?

해결책 2

No, that's the tricky part here: You'll have to store the information which union entry to use (e.g. using a single char member). Also note that both union entries might not necessarily point to the same locations (as you might expect, e.g. due to packing or endianess), so you can't just read one value and determine whether it's containing a valid value or not, since it's up to the compiler how to implement the struct in the union behind the scenes.

다른 팁

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"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top