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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:56:31+00:00 2026-05-25T13:56:31+00:00

I want to create a linked list, but I want the data field to

  • 0

I want to create a linked list, but I want the data field to be either int or float. I wrote some code like the following:

union int_or_float
{
    int int_member;
    float float_member;
};

struct node 
{
    union int_or_float data;
    struct node *next;
};

I want to write stuff like:

typedef struct node item; 

but how to specify which type in union do I like item to be?

  • 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-25T13:56:32+00:00Added an answer on May 25, 2026 at 1:56 pm

    That’s done automatically when you access the data. A union is nothing more than a slightly-less-kludgy way of casting a pointer.

    #include <stdio.h>
    
    union number {
      int i;
      float f;
    };
    
    struct node {
      union number data;
      struct node *next;
    };
    
    typedef struct node item;
    
    int main(void) {
      item myItem;
    
      myItem.data.i = 12; /* Now contains an int */
      myItem.data.f = 12.0; /* Now contains a float */
      printf("location of myItem.data is %p\n", &myItem.data);
      printf("location of myItem.data.f is %p\n", &myItem.data.f);  
      printf("myItem.data is %f\n", *(float *)(&myItem.data));
      printf("myItem.data.f is %f\n", myItem.data.f);
      printf("sizeof(myItem.data) is %lu\n", sizeof(*(float *)(&myItem.data)));
      printf("sizeof(myItem.data.f) is %lu\n", sizeof(myItem.data.f));
    
      return 0;
    }
    

    This will output

    location of myItem.data is 0x7fff5fbff9f0
    location of myItem.data.f is 0x7fff5fbff9f0
    myItem.data is 12.000000
    myItem.data.f is 12.000000
    sizeof(myItem.data) is 4
    sizeof(myItem.data.f) is 4
    

    run/tested on an Intel Mac with gcc version 4.2.1 (Apple Inc. build 5659)

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

Sidebar

Related Questions

I want to create something similar to a double linked list (but with arrays)
I need to create a Linked List from a text file that looks like
I am creating an Abstract Data Type, which create a doubly linked list (not
i want create a custom json data from the mssql 2008 results so that
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want create module which update list of usb devices automatically (not only mass
I want to create a new field (or two) in my table that is
I want to create a Silverlight app to extract and manipulate data from an
I need to create a completely generic linked list that can contain any type
I want to create a type using Type.GetType( nameOfTheType ), where nameOfTheType is some

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.