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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:23:52+00:00 2026-05-28T15:23:52+00:00

In a header outside of my control, there is this: typedef union { char

  • 0

In a header outside of my control, there is this:

typedef union {
    char * v_charp;
    int v_int;
    float v_float;
} value_t;

typedef struct var {
    char *name;
    value_t value;
} variable;

#define VARIABLE_DEF(Name, Value) {Name, {(char*)Value}}

They expect that in my code I’ll do something like this:

variable my_variables[2] = {
    VARIABLE_DEF("Variable 1", 1),
    VARIABLE_DEF("Variable 2", 2)
};

Whoever wrote this apparently didn’t consider that you might want to initialise the union with a floating-point literal. So I need to figure out how to convert a literal float to an integer of the same bit-pattern. If I could use an intermediate variable then it’d be easy:

float tmp;

variable my_variables[2] = {
    VARIABLE_DEF("Variable 1", tmp = 1.1f, *((unsigned int *)(&tmp))),
    VARIABLE_DEF("Variable 2", tmp = 2.2f, *((unsigned int *)(&tmp)))
};

But you can’t use variables in struct initialisers. What else can I do?

  • 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-28T15:23:53+00:00Added an answer on May 28, 2026 at 3:23 pm

    How about

    variable my_variables[2] = {
        VARIABLE_DEF("Variable 1", ((value_t){.v_float = 1.1f}.v_int)),
        VARIABLE_DEF("Variable 2", ((value_t){.v_float = 2.2f}.v_int)),
    };
    

    (Untested)

    On second thought, how about defining a more flexible alternative to VARIABLE_DEF and using that when needed?

    Something like

    #define VARIABLE_DEF_ALT(Name, Value) {Name, {Value}}
    #define VARIABLE_DEF_ALT2(Name, Field, Value) {Name, {.Field = Value}}
    
    variable my_variables[2] = {
        VARIABLE_DEF_ALT("Variable 1", .v_float = 1.1f),
        VARIABLE_DEF_ALT2("Variable 2", v_float, 2.2f),
    };
    

    should work.

    Or just skip the macro:

    variable my_variables[2] = {
        {"Variable 1", {.v_float = 1.1f}},
        {"Variable 2", {.v_float = 2.2f}},
    };
    

    — is that actually the macro, or are you simplifying a much more complicated case for this discussion?

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

Sidebar

Related Questions

I'm displaying images from outside my web root, like this: header('Content-type:image/png'); readfile($fullpath); The content-type:
header file: private: vector<int*>* nums; public slots: void buttonClicked(); cpp file: NewWindow(){ int one
header('Access-Control-Allow-Origin: *'); $tmpFile = 'tmpFile.txt'; $val=http://rss.news.yahoo.com/rss/topstories; $curlHandle = curl_init($val); $filePointer = fopen($tmpFile, w); curl_setopt($curlHandle,
The header Cache-Control: max-age=0 implies that the content is considered stale (and must be
I am using the apple example code SpleakHere . in this code there is
ListView.ColumnClick doesn't seem to fire for clicks in the header area that is outside
I have a basic C++ class .The header looks like this: #pragma once class
Is there a way to group data into columns using a data bound control
I'm trying to make a header that goes outside the borders of my sidebar
Im wondering how to get a label control that is in a header template

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.