C union containing only one struct
I have started today to program on a PIC16f88, and found that the header for its registers contains a union
that only contains a struct
:
extern volatile unsigned char ANSEL __at(0x09B);
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
Does it provide any benefits to enclose the struct
inside a union
that only contains that struct
?
Its access I guess is going to be exactly the same as if it were a simple struct
(because the struct
is anonymous):
ANSELbits.ANS4 = 0;
c struct unions
add a comment |
I have started today to program on a PIC16f88, and found that the header for its registers contains a union
that only contains a struct
:
extern volatile unsigned char ANSEL __at(0x09B);
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
Does it provide any benefits to enclose the struct
inside a union
that only contains that struct
?
Its access I guess is going to be exactly the same as if it were a simple struct
(because the struct
is anonymous):
ANSELbits.ANS4 = 0;
c struct unions
1
Would you mind confirming that you have or haven't omitted another member of theunion
?
– Bathsheba
3 hours ago
I copied exactly the code from the header. Nothing omitted.
– Cacahuete Frito
3 hours ago
add a comment |
I have started today to program on a PIC16f88, and found that the header for its registers contains a union
that only contains a struct
:
extern volatile unsigned char ANSEL __at(0x09B);
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
Does it provide any benefits to enclose the struct
inside a union
that only contains that struct
?
Its access I guess is going to be exactly the same as if it were a simple struct
(because the struct
is anonymous):
ANSELbits.ANS4 = 0;
c struct unions
I have started today to program on a PIC16f88, and found that the header for its registers contains a union
that only contains a struct
:
extern volatile unsigned char ANSEL __at(0x09B);
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
Does it provide any benefits to enclose the struct
inside a union
that only contains that struct
?
Its access I guess is going to be exactly the same as if it were a simple struct
(because the struct
is anonymous):
ANSELbits.ANS4 = 0;
c struct unions
c struct unions
edited 3 hours ago
Cacahuete Frito
asked 3 hours ago
Cacahuete FritoCacahuete Frito
1097
1097
1
Would you mind confirming that you have or haven't omitted another member of theunion
?
– Bathsheba
3 hours ago
I copied exactly the code from the header. Nothing omitted.
– Cacahuete Frito
3 hours ago
add a comment |
1
Would you mind confirming that you have or haven't omitted another member of theunion
?
– Bathsheba
3 hours ago
I copied exactly the code from the header. Nothing omitted.
– Cacahuete Frito
3 hours ago
1
1
Would you mind confirming that you have or haven't omitted another member of the
union
?– Bathsheba
3 hours ago
Would you mind confirming that you have or haven't omitted another member of the
union
?– Bathsheba
3 hours ago
I copied exactly the code from the header. Nothing omitted.
– Cacahuete Frito
3 hours ago
I copied exactly the code from the header. Nothing omitted.
– Cacahuete Frito
3 hours ago
add a comment |
2 Answers
2
active
oldest
votes
There is no benefit in standard portable C.
But code like this is used to circumvent (in a non-portable way) all the type checking that your C compiler will make.
You are then empowered to set all the members of the underlying struct
in one go, which is useful in this case as it contains a lot of bit fields.
2
any porability considerations are voided when I seeextern volatile ANSELbits_t ANSELbits __at(0x09B);
which is specific to Keil compiler used by some programmers in the uC programming (expensive and IMO not very good), but supported directly by ARM
– P__J__
3 hours ago
1
@CacahueteFrito: in which case I'm pretty convinced I'm correct. Remove theunion
and see what happens. Compiler warnings galore I'd expect.
– Bathsheba
3 hours ago
1
@P__J__: Trust me, this is an old-fashioned idiom. Not much more I can say really!
– Bathsheba
3 hours ago
2
@CacahueteFrito PIC uC? - my condolences :). LOL - the original programmer did not know how to use unions and has found this "workaround" :)
– P__J__
3 hours ago
3
Using bit-fields means the code is not portable, period. Almost everything about bit-fields is implementation-defined.
– Jonathan Leffler
3 hours ago
|
show 7 more comments
It does not make any difference if you wrap and I suppose that someone has forgoten to add another member (or did not copy-paste everything) as in the declaration below. No warnings will be suppressed.
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
uint8_t d8;
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
BTW if the struct has to fit in 1 byte (8 bits) this declaration is wrong and uint_t type should be used instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54390039%2fc-union-containing-only-one-struct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no benefit in standard portable C.
But code like this is used to circumvent (in a non-portable way) all the type checking that your C compiler will make.
You are then empowered to set all the members of the underlying struct
in one go, which is useful in this case as it contains a lot of bit fields.
2
any porability considerations are voided when I seeextern volatile ANSELbits_t ANSELbits __at(0x09B);
which is specific to Keil compiler used by some programmers in the uC programming (expensive and IMO not very good), but supported directly by ARM
– P__J__
3 hours ago
1
@CacahueteFrito: in which case I'm pretty convinced I'm correct. Remove theunion
and see what happens. Compiler warnings galore I'd expect.
– Bathsheba
3 hours ago
1
@P__J__: Trust me, this is an old-fashioned idiom. Not much more I can say really!
– Bathsheba
3 hours ago
2
@CacahueteFrito PIC uC? - my condolences :). LOL - the original programmer did not know how to use unions and has found this "workaround" :)
– P__J__
3 hours ago
3
Using bit-fields means the code is not portable, period. Almost everything about bit-fields is implementation-defined.
– Jonathan Leffler
3 hours ago
|
show 7 more comments
There is no benefit in standard portable C.
But code like this is used to circumvent (in a non-portable way) all the type checking that your C compiler will make.
You are then empowered to set all the members of the underlying struct
in one go, which is useful in this case as it contains a lot of bit fields.
2
any porability considerations are voided when I seeextern volatile ANSELbits_t ANSELbits __at(0x09B);
which is specific to Keil compiler used by some programmers in the uC programming (expensive and IMO not very good), but supported directly by ARM
– P__J__
3 hours ago
1
@CacahueteFrito: in which case I'm pretty convinced I'm correct. Remove theunion
and see what happens. Compiler warnings galore I'd expect.
– Bathsheba
3 hours ago
1
@P__J__: Trust me, this is an old-fashioned idiom. Not much more I can say really!
– Bathsheba
3 hours ago
2
@CacahueteFrito PIC uC? - my condolences :). LOL - the original programmer did not know how to use unions and has found this "workaround" :)
– P__J__
3 hours ago
3
Using bit-fields means the code is not portable, period. Almost everything about bit-fields is implementation-defined.
– Jonathan Leffler
3 hours ago
|
show 7 more comments
There is no benefit in standard portable C.
But code like this is used to circumvent (in a non-portable way) all the type checking that your C compiler will make.
You are then empowered to set all the members of the underlying struct
in one go, which is useful in this case as it contains a lot of bit fields.
There is no benefit in standard portable C.
But code like this is used to circumvent (in a non-portable way) all the type checking that your C compiler will make.
You are then empowered to set all the members of the underlying struct
in one go, which is useful in this case as it contains a lot of bit fields.
answered 3 hours ago
BathshebaBathsheba
177k27253376
177k27253376
2
any porability considerations are voided when I seeextern volatile ANSELbits_t ANSELbits __at(0x09B);
which is specific to Keil compiler used by some programmers in the uC programming (expensive and IMO not very good), but supported directly by ARM
– P__J__
3 hours ago
1
@CacahueteFrito: in which case I'm pretty convinced I'm correct. Remove theunion
and see what happens. Compiler warnings galore I'd expect.
– Bathsheba
3 hours ago
1
@P__J__: Trust me, this is an old-fashioned idiom. Not much more I can say really!
– Bathsheba
3 hours ago
2
@CacahueteFrito PIC uC? - my condolences :). LOL - the original programmer did not know how to use unions and has found this "workaround" :)
– P__J__
3 hours ago
3
Using bit-fields means the code is not portable, period. Almost everything about bit-fields is implementation-defined.
– Jonathan Leffler
3 hours ago
|
show 7 more comments
2
any porability considerations are voided when I seeextern volatile ANSELbits_t ANSELbits __at(0x09B);
which is specific to Keil compiler used by some programmers in the uC programming (expensive and IMO not very good), but supported directly by ARM
– P__J__
3 hours ago
1
@CacahueteFrito: in which case I'm pretty convinced I'm correct. Remove theunion
and see what happens. Compiler warnings galore I'd expect.
– Bathsheba
3 hours ago
1
@P__J__: Trust me, this is an old-fashioned idiom. Not much more I can say really!
– Bathsheba
3 hours ago
2
@CacahueteFrito PIC uC? - my condolences :). LOL - the original programmer did not know how to use unions and has found this "workaround" :)
– P__J__
3 hours ago
3
Using bit-fields means the code is not portable, period. Almost everything about bit-fields is implementation-defined.
– Jonathan Leffler
3 hours ago
2
2
any porability considerations are voided when I see
extern volatile ANSELbits_t ANSELbits __at(0x09B);
which is specific to Keil compiler used by some programmers in the uC programming (expensive and IMO not very good), but supported directly by ARM– P__J__
3 hours ago
any porability considerations are voided when I see
extern volatile ANSELbits_t ANSELbits __at(0x09B);
which is specific to Keil compiler used by some programmers in the uC programming (expensive and IMO not very good), but supported directly by ARM– P__J__
3 hours ago
1
1
@CacahueteFrito: in which case I'm pretty convinced I'm correct. Remove the
union
and see what happens. Compiler warnings galore I'd expect.– Bathsheba
3 hours ago
@CacahueteFrito: in which case I'm pretty convinced I'm correct. Remove the
union
and see what happens. Compiler warnings galore I'd expect.– Bathsheba
3 hours ago
1
1
@P__J__: Trust me, this is an old-fashioned idiom. Not much more I can say really!
– Bathsheba
3 hours ago
@P__J__: Trust me, this is an old-fashioned idiom. Not much more I can say really!
– Bathsheba
3 hours ago
2
2
@CacahueteFrito PIC uC? - my condolences :). LOL - the original programmer did not know how to use unions and has found this "workaround" :)
– P__J__
3 hours ago
@CacahueteFrito PIC uC? - my condolences :). LOL - the original programmer did not know how to use unions and has found this "workaround" :)
– P__J__
3 hours ago
3
3
Using bit-fields means the code is not portable, period. Almost everything about bit-fields is implementation-defined.
– Jonathan Leffler
3 hours ago
Using bit-fields means the code is not portable, period. Almost everything about bit-fields is implementation-defined.
– Jonathan Leffler
3 hours ago
|
show 7 more comments
It does not make any difference if you wrap and I suppose that someone has forgoten to add another member (or did not copy-paste everything) as in the declaration below. No warnings will be suppressed.
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
uint8_t d8;
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
BTW if the struct has to fit in 1 byte (8 bits) this declaration is wrong and uint_t type should be used instead.
add a comment |
It does not make any difference if you wrap and I suppose that someone has forgoten to add another member (or did not copy-paste everything) as in the declaration below. No warnings will be suppressed.
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
uint8_t d8;
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
BTW if the struct has to fit in 1 byte (8 bits) this declaration is wrong and uint_t type should be used instead.
add a comment |
It does not make any difference if you wrap and I suppose that someone has forgoten to add another member (or did not copy-paste everything) as in the declaration below. No warnings will be suppressed.
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
uint8_t d8;
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
BTW if the struct has to fit in 1 byte (8 bits) this declaration is wrong and uint_t type should be used instead.
It does not make any difference if you wrap and I suppose that someone has forgoten to add another member (or did not copy-paste everything) as in the declaration below. No warnings will be suppressed.
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
uint8_t d8;
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
BTW if the struct has to fit in 1 byte (8 bits) this declaration is wrong and uint_t type should be used instead.
answered 3 hours ago
P__J__P__J__
10.1k2723
10.1k2723
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54390039%2fc-union-containing-only-one-struct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Would you mind confirming that you have or haven't omitted another member of the
union
?– Bathsheba
3 hours ago
I copied exactly the code from the header. Nothing omitted.
– Cacahuete Frito
3 hours ago