Initializing floating value of UNION in C
I have below union,
typedef union
{
struct
{
float x;
float y;
float z;
float Backup;
} pt;
float Max[4];
} Var3D;
When I try to initialize above union like Var3D= { 0.0, 0.0, 0.0, 0.0 };
It shows build error like
suggest braces around initialization of subobject
[-Werror,-Wmissing-braces]
Var3D= {0.0, 0.0, 0.0, 0.0};
How can I fix this?
c
|
show 3 more comments
I have below union,
typedef union
{
struct
{
float x;
float y;
float z;
float Backup;
} pt;
float Max[4];
} Var3D;
When I try to initialize above union like Var3D= { 0.0, 0.0, 0.0, 0.0 };
It shows build error like
suggest braces around initialization of subobject
[-Werror,-Wmissing-braces]
Var3D= {0.0, 0.0, 0.0, 0.0};
How can I fix this?
c
What happens when your follow the advice "suggest braces around initialization of subobject" and put{}
around{0.0, 0.0, 0.0, 0.0}
?
– chux
Nov 24 '18 at 6:50
1
It would help if you gave an actual code sample.3DVar
is not a valid identifier, so yourtypedef
would not compile. In any event, assuming your ACTUAL code (as distinct from the code you have posted) has a validtypedef
name other than3DVar
, try doing as the error message suggests:YourType some_variable = {{0.0f, 0.0f, 0.0f, 0.0f}};
. Note there are two sets of{}
. In future, read up on how to ask a question in a way that is more likely to get useful responses, in particular provision of a Minimal, Complete, and Verifiable example.
– Peter
Nov 24 '18 at 6:52
Adding braces like below resolves, {{0.0, 0.0, 0.0, 0.0}} but i don't understand the {{0.0, 0.0, 0.0, 0.0}}; --> Resolve this. But {0.0, 0.0, 0.0, 0.0} is already initialization. I don't understand this clearly. Braces are already present and this is of union type. So when we initialize larger size member.
– Perry
Nov 24 '18 at 7:07
2
Note that you can't use the type name3Dvar
— identifiers cannot start with digits. You also don't show a valid variable definition. Maybe you need to use a type name such asVar3D
and then the variable definition would beVar3D var3d = { .pt = { 0.0, 0.0, 0.0, 0.0 } };
using a designated initializer, or you can drop the.pt =
and leave the doubled braces to initialize correctly. Technically, that undesignated notation initializes the first element of the union, which ispt
. You could designate the alternative with.Max =
in place of.pt =
. The net result is the same here.
– Jonathan Leffler
Nov 24 '18 at 7:15
2
@n.m.: In C, unlike C++, it is not undefined to access a member other than the last one stored. Per the C standard, the bytes are reinterpreted as the new type. There may be implementation-defined effects.
– Eric Postpischil
Nov 24 '18 at 12:06
|
show 3 more comments
I have below union,
typedef union
{
struct
{
float x;
float y;
float z;
float Backup;
} pt;
float Max[4];
} Var3D;
When I try to initialize above union like Var3D= { 0.0, 0.0, 0.0, 0.0 };
It shows build error like
suggest braces around initialization of subobject
[-Werror,-Wmissing-braces]
Var3D= {0.0, 0.0, 0.0, 0.0};
How can I fix this?
c
I have below union,
typedef union
{
struct
{
float x;
float y;
float z;
float Backup;
} pt;
float Max[4];
} Var3D;
When I try to initialize above union like Var3D= { 0.0, 0.0, 0.0, 0.0 };
It shows build error like
suggest braces around initialization of subobject
[-Werror,-Wmissing-braces]
Var3D= {0.0, 0.0, 0.0, 0.0};
How can I fix this?
c
c
edited Nov 24 '18 at 7:24
Perry
asked Nov 24 '18 at 6:46
PerryPerry
11
11
What happens when your follow the advice "suggest braces around initialization of subobject" and put{}
around{0.0, 0.0, 0.0, 0.0}
?
– chux
Nov 24 '18 at 6:50
1
It would help if you gave an actual code sample.3DVar
is not a valid identifier, so yourtypedef
would not compile. In any event, assuming your ACTUAL code (as distinct from the code you have posted) has a validtypedef
name other than3DVar
, try doing as the error message suggests:YourType some_variable = {{0.0f, 0.0f, 0.0f, 0.0f}};
. Note there are two sets of{}
. In future, read up on how to ask a question in a way that is more likely to get useful responses, in particular provision of a Minimal, Complete, and Verifiable example.
– Peter
Nov 24 '18 at 6:52
Adding braces like below resolves, {{0.0, 0.0, 0.0, 0.0}} but i don't understand the {{0.0, 0.0, 0.0, 0.0}}; --> Resolve this. But {0.0, 0.0, 0.0, 0.0} is already initialization. I don't understand this clearly. Braces are already present and this is of union type. So when we initialize larger size member.
– Perry
Nov 24 '18 at 7:07
2
Note that you can't use the type name3Dvar
— identifiers cannot start with digits. You also don't show a valid variable definition. Maybe you need to use a type name such asVar3D
and then the variable definition would beVar3D var3d = { .pt = { 0.0, 0.0, 0.0, 0.0 } };
using a designated initializer, or you can drop the.pt =
and leave the doubled braces to initialize correctly. Technically, that undesignated notation initializes the first element of the union, which ispt
. You could designate the alternative with.Max =
in place of.pt =
. The net result is the same here.
– Jonathan Leffler
Nov 24 '18 at 7:15
2
@n.m.: In C, unlike C++, it is not undefined to access a member other than the last one stored. Per the C standard, the bytes are reinterpreted as the new type. There may be implementation-defined effects.
– Eric Postpischil
Nov 24 '18 at 12:06
|
show 3 more comments
What happens when your follow the advice "suggest braces around initialization of subobject" and put{}
around{0.0, 0.0, 0.0, 0.0}
?
– chux
Nov 24 '18 at 6:50
1
It would help if you gave an actual code sample.3DVar
is not a valid identifier, so yourtypedef
would not compile. In any event, assuming your ACTUAL code (as distinct from the code you have posted) has a validtypedef
name other than3DVar
, try doing as the error message suggests:YourType some_variable = {{0.0f, 0.0f, 0.0f, 0.0f}};
. Note there are two sets of{}
. In future, read up on how to ask a question in a way that is more likely to get useful responses, in particular provision of a Minimal, Complete, and Verifiable example.
– Peter
Nov 24 '18 at 6:52
Adding braces like below resolves, {{0.0, 0.0, 0.0, 0.0}} but i don't understand the {{0.0, 0.0, 0.0, 0.0}}; --> Resolve this. But {0.0, 0.0, 0.0, 0.0} is already initialization. I don't understand this clearly. Braces are already present and this is of union type. So when we initialize larger size member.
– Perry
Nov 24 '18 at 7:07
2
Note that you can't use the type name3Dvar
— identifiers cannot start with digits. You also don't show a valid variable definition. Maybe you need to use a type name such asVar3D
and then the variable definition would beVar3D var3d = { .pt = { 0.0, 0.0, 0.0, 0.0 } };
using a designated initializer, or you can drop the.pt =
and leave the doubled braces to initialize correctly. Technically, that undesignated notation initializes the first element of the union, which ispt
. You could designate the alternative with.Max =
in place of.pt =
. The net result is the same here.
– Jonathan Leffler
Nov 24 '18 at 7:15
2
@n.m.: In C, unlike C++, it is not undefined to access a member other than the last one stored. Per the C standard, the bytes are reinterpreted as the new type. There may be implementation-defined effects.
– Eric Postpischil
Nov 24 '18 at 12:06
What happens when your follow the advice "suggest braces around initialization of subobject" and put
{}
around {0.0, 0.0, 0.0, 0.0}
?– chux
Nov 24 '18 at 6:50
What happens when your follow the advice "suggest braces around initialization of subobject" and put
{}
around {0.0, 0.0, 0.0, 0.0}
?– chux
Nov 24 '18 at 6:50
1
1
It would help if you gave an actual code sample.
3DVar
is not a valid identifier, so your typedef
would not compile. In any event, assuming your ACTUAL code (as distinct from the code you have posted) has a valid typedef
name other than 3DVar
, try doing as the error message suggests: YourType some_variable = {{0.0f, 0.0f, 0.0f, 0.0f}};
. Note there are two sets of {}
. In future, read up on how to ask a question in a way that is more likely to get useful responses, in particular provision of a Minimal, Complete, and Verifiable example.– Peter
Nov 24 '18 at 6:52
It would help if you gave an actual code sample.
3DVar
is not a valid identifier, so your typedef
would not compile. In any event, assuming your ACTUAL code (as distinct from the code you have posted) has a valid typedef
name other than 3DVar
, try doing as the error message suggests: YourType some_variable = {{0.0f, 0.0f, 0.0f, 0.0f}};
. Note there are two sets of {}
. In future, read up on how to ask a question in a way that is more likely to get useful responses, in particular provision of a Minimal, Complete, and Verifiable example.– Peter
Nov 24 '18 at 6:52
Adding braces like below resolves, {{0.0, 0.0, 0.0, 0.0}} but i don't understand the {{0.0, 0.0, 0.0, 0.0}}; --> Resolve this. But {0.0, 0.0, 0.0, 0.0} is already initialization. I don't understand this clearly. Braces are already present and this is of union type. So when we initialize larger size member.
– Perry
Nov 24 '18 at 7:07
Adding braces like below resolves, {{0.0, 0.0, 0.0, 0.0}} but i don't understand the {{0.0, 0.0, 0.0, 0.0}}; --> Resolve this. But {0.0, 0.0, 0.0, 0.0} is already initialization. I don't understand this clearly. Braces are already present and this is of union type. So when we initialize larger size member.
– Perry
Nov 24 '18 at 7:07
2
2
Note that you can't use the type name
3Dvar
— identifiers cannot start with digits. You also don't show a valid variable definition. Maybe you need to use a type name such as Var3D
and then the variable definition would be Var3D var3d = { .pt = { 0.0, 0.0, 0.0, 0.0 } };
using a designated initializer, or you can drop the .pt =
and leave the doubled braces to initialize correctly. Technically, that undesignated notation initializes the first element of the union, which is pt
. You could designate the alternative with .Max =
in place of .pt =
. The net result is the same here.– Jonathan Leffler
Nov 24 '18 at 7:15
Note that you can't use the type name
3Dvar
— identifiers cannot start with digits. You also don't show a valid variable definition. Maybe you need to use a type name such as Var3D
and then the variable definition would be Var3D var3d = { .pt = { 0.0, 0.0, 0.0, 0.0 } };
using a designated initializer, or you can drop the .pt =
and leave the doubled braces to initialize correctly. Technically, that undesignated notation initializes the first element of the union, which is pt
. You could designate the alternative with .Max =
in place of .pt =
. The net result is the same here.– Jonathan Leffler
Nov 24 '18 at 7:15
2
2
@n.m.: In C, unlike C++, it is not undefined to access a member other than the last one stored. Per the C standard, the bytes are reinterpreted as the new type. There may be implementation-defined effects.
– Eric Postpischil
Nov 24 '18 at 12:06
@n.m.: In C, unlike C++, it is not undefined to access a member other than the last one stored. Per the C standard, the bytes are reinterpreted as the new type. There may be implementation-defined effects.
– Eric Postpischil
Nov 24 '18 at 12:06
|
show 3 more comments
1 Answer
1
active
oldest
votes
So make an answer:
As Peter mentioned you need two pair of braces: one for the union and one for the array included in the union.
So this initialization should work:
Var3D= {{ 0.0, 0.0, 0.0, 0.0 }};
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%2f53455848%2finitializing-floating-value-of-union-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So make an answer:
As Peter mentioned you need two pair of braces: one for the union and one for the array included in the union.
So this initialization should work:
Var3D= {{ 0.0, 0.0, 0.0, 0.0 }};
add a comment |
So make an answer:
As Peter mentioned you need two pair of braces: one for the union and one for the array included in the union.
So this initialization should work:
Var3D= {{ 0.0, 0.0, 0.0, 0.0 }};
add a comment |
So make an answer:
As Peter mentioned you need two pair of braces: one for the union and one for the array included in the union.
So this initialization should work:
Var3D= {{ 0.0, 0.0, 0.0, 0.0 }};
So make an answer:
As Peter mentioned you need two pair of braces: one for the union and one for the array included in the union.
So this initialization should work:
Var3D= {{ 0.0, 0.0, 0.0, 0.0 }};
answered Dec 4 '18 at 13:42
MikeMike
2,0031722
2,0031722
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%2f53455848%2finitializing-floating-value-of-union-in-c%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
What happens when your follow the advice "suggest braces around initialization of subobject" and put
{}
around{0.0, 0.0, 0.0, 0.0}
?– chux
Nov 24 '18 at 6:50
1
It would help if you gave an actual code sample.
3DVar
is not a valid identifier, so yourtypedef
would not compile. In any event, assuming your ACTUAL code (as distinct from the code you have posted) has a validtypedef
name other than3DVar
, try doing as the error message suggests:YourType some_variable = {{0.0f, 0.0f, 0.0f, 0.0f}};
. Note there are two sets of{}
. In future, read up on how to ask a question in a way that is more likely to get useful responses, in particular provision of a Minimal, Complete, and Verifiable example.– Peter
Nov 24 '18 at 6:52
Adding braces like below resolves, {{0.0, 0.0, 0.0, 0.0}} but i don't understand the {{0.0, 0.0, 0.0, 0.0}}; --> Resolve this. But {0.0, 0.0, 0.0, 0.0} is already initialization. I don't understand this clearly. Braces are already present and this is of union type. So when we initialize larger size member.
– Perry
Nov 24 '18 at 7:07
2
Note that you can't use the type name
3Dvar
— identifiers cannot start with digits. You also don't show a valid variable definition. Maybe you need to use a type name such asVar3D
and then the variable definition would beVar3D var3d = { .pt = { 0.0, 0.0, 0.0, 0.0 } };
using a designated initializer, or you can drop the.pt =
and leave the doubled braces to initialize correctly. Technically, that undesignated notation initializes the first element of the union, which ispt
. You could designate the alternative with.Max =
in place of.pt =
. The net result is the same here.– Jonathan Leffler
Nov 24 '18 at 7:15
2
@n.m.: In C, unlike C++, it is not undefined to access a member other than the last one stored. Per the C standard, the bytes are reinterpreted as the new type. There may be implementation-defined effects.
– Eric Postpischil
Nov 24 '18 at 12:06