Initializing floating value of UNION in C












0















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?










share|improve this question

























  • 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 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








  • 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






  • 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
















0















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?










share|improve this question

























  • 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 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








  • 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






  • 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














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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








  • 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






  • 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






  • 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











  • 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 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





    @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












1 Answer
1






active

oldest

votes


















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 }};





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    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 }};





    share|improve this answer




























      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 }};





      share|improve this answer


























        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 }};





        share|improve this answer













        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 }};






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 '18 at 13:42









        MikeMike

        2,0031722




        2,0031722
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'