Can a union in standard C "skip" members?
-
The following is an example of a simple struct union:
typedef union _UINT32_PART_ {
uint32_t dwWord;struct {
uint16_t dwMSB;
uint16_t dwLSB;
} hw;struct {
uint8_t byMSB;
uint8_t byMSBL;
uint8_t byLSBH;
uint8_t byLSB;
} b;
} DWORD_PART;Is is possible to somehow omit, for example dwMSB and byLSBH, without making the subsequent members point to the wrong things? So, basically this is what I want:
typedef union _UINT32_PART_ {
uint32_t dwWord;struct {
// 2 bytes empty gap
uint16_t dwLSB; // This must still point to the 2nd word, not the 1st!
} hw;struct {
uint8_t byMSB;
uint8_t byMSBL;
// 1 byte empty gap
uint8_t byLSB; // This must still point to the 4th byte, not the 3rd!
} b;
} DWORD_PART; -
The following is an example of a simple struct union:
typedef union _UINT32_PART_ {
uint32_t dwWord;struct {
uint16_t dwMSB;
uint16_t dwLSB;
} hw;struct {
uint8_t byMSB;
uint8_t byMSBL;
uint8_t byLSBH;
uint8_t byLSB;
} b;
} DWORD_PART;Is is possible to somehow omit, for example dwMSB and byLSBH, without making the subsequent members point to the wrong things? So, basically this is what I want:
typedef union _UINT32_PART_ {
uint32_t dwWord;struct {
// 2 bytes empty gap
uint16_t dwLSB; // This must still point to the 2nd word, not the 1st!
} hw;struct {
uint8_t byMSB;
uint8_t byMSBL;
// 1 byte empty gap
uint8_t byLSB; // This must still point to the 4th byte, not the 3rd!
} b;
} DWORD_PART; -
Sorry, but I do not understand what you hope to achieve. A
struct
is just a description of a block of memory that contains a fixed set ofbyte
s,word
s etc. If it is 20byte
s long then you must describe each part exactly so the compiler can calculate the offsets correctly. Leaving blank space and expecting the compiler to guess what is missing makes no sense. -
Sorry, but I do not understand what you hope to achieve. A
struct
is just a description of a block of memory that contains a fixed set ofbyte
s,word
s etc. If it is 20byte
s long then you must describe each part exactly so the compiler can calculate the offsets correctly. Leaving blank space and expecting the compiler to guess what is missing makes no sense.Yes, but just like you can choose to only initialize parts of a struct:
struct demo_s {
int first;
int second;
int third;
};
struct demo_s demo2 = { .second = 2, .third = 3 };I was hoping that some union names (hw or b, in the example in my first post) could omit having a reference.
-
Yes, but just like you can choose to only initialize parts of a struct:
struct demo_s {
int first;
int second;
int third;
};
struct demo_s demo2 = { .second = 2, .third = 3 };I was hoping that some union names (hw or b, in the example in my first post) could omit having a reference.
-
Whether you initialise all or part of the struct before using it does not matter, except for your code. But you must still define it exactly and completely.
-
Yes, but only struct members relevant to the application level should be initialized at the application level. The low-level driver struct members should be initialized at the low-level driver level.
-
Sorry, but I do not understand what you hope to achieve. A
struct
is just a description of a block of memory that contains a fixed set ofbyte
s,word
s etc. If it is 20byte
s long then you must describe each part exactly so the compiler can calculate the offsets correctly. Leaving blank space and expecting the compiler to guess what is missing makes no sense.successful freelancers swear by exercises — washing your face, putting on actual garments and setting telephone limitations — and people recommendations growth to consuming. nutritional therapist and longtime paintings-from-homer wilma macdonald informed huffpost that snacking proper here and there is the biggest mistake she sees new a ways flung workers make. she recommends putting in your day like several normal operating day, with a lunch damage, a few smaller breaks and a focus at the maximum essential meal: breakfast. https://www.vipblogweb.com
-
The following is an example of a simple struct union:
typedef union _UINT32_PART_ {
uint32_t dwWord;struct {
uint16_t dwMSB;
uint16_t dwLSB;
} hw;struct {
uint8_t byMSB;
uint8_t byMSBL;
uint8_t byLSBH;
uint8_t byLSB;
} b;
} DWORD_PART;Is is possible to somehow omit, for example dwMSB and byLSBH, without making the subsequent members point to the wrong things? So, basically this is what I want:
typedef union _UINT32_PART_ {
uint32_t dwWord;struct {
// 2 bytes empty gap
uint16_t dwLSB; // This must still point to the 2nd word, not the 1st!
} hw;struct {
uint8_t byMSB;
uint8_t byMSBL;
// 1 byte empty gap
uint8_t byLSB; // This must still point to the 4th byte, not the 3rd!
} b;
} DWORD_PART;