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 7593697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:14:02+00:00 2026-05-30T21:14:02+00:00

Is it possible to determine what type a union contain if there are several

  • 0

Is it possible to determine what type a union contain if there are several possible choices?

typedef union
{
    char     charArr[SIZE];
    int      intVal;
    float    floatVal;
} VALUE_TYPE;

VALUE_TYPE number;
number.intVal = 8;

How to know what the union contain here if the value was set from somewhere else?

  • 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-05-30T21:14:04+00:00Added an answer on May 30, 2026 at 9:14 pm

    It’s right that you cannot do this kind of thing out of the box.

    A common way to workaround this is that you can add a type along with your union. For instance, it could be :

    enum { charArr_type, float_type, int_type } VALUE_TYPE;
    typedef union
    {
        char     charArr[SIZE];
        int      intVal;
        float    floatVal;
    } VALUE;
    struct my_value { 
       VALUE val,
       VALUE_TYPE val_type
    }
    

    After, you’ll just have to update type when you assign your struct :

    my_value number;
    number.val.intVal = 8;
    number.val.val_type = is_int
    

    It’s a common modern pattern when you need to have a common type capable of storing nearly anything.

    For instance, you can found it everywhere in PHP source code. This is how they store different value types in the same object. See this page for more detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to determine the type of a local variable at runtime in
Given a FILE*, is it possible to determine the underlying type? That is, is
In C# 3.0, is it possible to determine whether an instance of Type represents
Is it possible to determine an object type's alias(es) through reflection? Given the following
Possible Duplicate: .NET: Determine the type of this class in its static method How
Possible Duplicate: How to determine the attached type from within a custom attribute? I
Possible Duplicate: How to determine the class of a generic type? I have a
Is there any portable way to determine what the maximum possible alignment for any
Is it possible to determine from the System.ServiceModel.Syndication.SyndicationFeed instance what type of SyndicationFeed is
I know that it's possible to determine the data connection type (WIFI/or cellular) through

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.