Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:02:37+00:00 2026-06-06T23:02:37+00:00

Possible Duplicate: Can a union be initialized in the declaration? I’ve looked all over

  • 0

Possible Duplicate:
Can a union be initialized in the declaration?

I’ve looked all over the internet and can’t find an example of how to set the value of a union within a struct at compile time and I’m hoping that you guys and gals can help me out. For instance, a simple struct would be :

typedef enum {
     typeFloat,
     typeInt
} Type;

typedef struct myStruct { 
     Type     elementType;
     int      valueInt;
     float    valueFloat;
} myStruct;

and then you could declare a local variable with :

myStruct structEx = {typeInt, 349, 0};

or

myStruct structEx = {typeFloat, 0, 349.349};

How would you do the same if the struct was declared as :

typedef struct myStruct {
     Type     elementType;
     union value {
          int     valueInt;
          float   valueFloat;
     } value;   
} myStruct;

The “value” will be either a float or an int with the “elementType” allowing it to know which it was.

I know you can set it during runtime with :

myStruct structEx;
structEx.elementType = typeInt;
structEx.value.valueInt = 349;

but I haven’t found a way to do it as above with the struct.

Thanks in advance.

Edit : This is duplicate. I should’ve been using the word “initialization” and it would’ve taken me straight to that one. My Google-Fu must be weak today. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:02:38+00:00Added an answer on June 6, 2026 at 11:02 pm

    How about:

    myStruct structEx = {
        .elementType = 0,
        .value = {
            .valueInt = 42
        }
    };
    

    Or maybe

    myStruct structEx = {
        .elementType = 0,
        .value.valueInt = 42
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can I set a property value with Reflection? How do I set
Possible Duplicate: Can I find the console width with Java? For example, when you
Possible Duplicate: Can I set a breakpoint on 'memory access' in GDB? I want
Possible Duplicate: Can I change all my links to just //? I came across
Possible Duplicate: Set all bytes of int to (unsigned char)0, guaranteed to represent zero?
Possible Duplicate: Can't find socket.io.js Well when using socket.io you need to include the
Possible Duplicate: Can I fetch the value of a non-standard CSS property via Javascript?
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Can I run from command line program created by Eclipse? I am
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.