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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:08:01+00:00 2026-06-03T03:08:01+00:00

After implementing the data structure suggested in this thread , I decided to fill

  • 0

After implementing the data structure suggested in this thread, I decided to fill it with some test values.

The code is

typedef enum {car, gun, bullet} itemtype;

typedef struct bullettype {
    unsigned short velocity;
    unsigned short kinetic_energy;
} Bullet;

typedef struct cartype {
    unsigned short maxspeed;
} Car;

typedef enum {handgun, shotgun, machinegun} weapontype;

typedef struct firearmtype {
    weapontype type;
    char capacity;
} Firearm;

typedef struct gameitem {
    itemtype type;
    char* name;
    unsigned short bulk; 
    unsigned short weight;
    union {
        Car c;
        Bullet b;
        Firearm f;
    };
} Item;

int main() {
    Item *objects = malloc(50 *sizeof(Item));
    objects[0] = (Item) {gun, "a gun", 500, 5000, (Firearm) {handgun, 10}};
    objects[1] = (Item) {car, "a car", 3500, 4000, (Car) {200}};
    objects[2] = (Item) {bullet, "a bullet", 200, 3000, (Bullet) {300, 5000}};
    free(objects);
    return 0;
}

But when I compile it, I get

itemlisttest.c:40:2: error: incompatible types when initializing type ‘short unsigned int’ using type ‘Firearm’
itemlisttest.c:42:3: error: incompatible types when initializing type ‘short unsigned int’ using type ‘Bullet’

Which means the car item is working fine. And it’s listed first in the union inside the struct. So I decided to swap Car and Bullet in the union like this

union {
    Bullet b;
    Car c;
    Firearm f;
};

And the compiler error is now

itemlisttest.c:40:2: error: incompatible types when initializing type ‘short unsigned int’ using type ‘Firearm’
itemlisttest.c:41:2: error: incompatible types when initializing type ‘short unsigned int’ using type ‘Car’

Does this have to do with the way I’m initializing the structs? I’m doing it like that because it will be a long list and 1000 object[a].name = "name" lines don’t look pretty at all.

  • 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-03T03:08:01+00:00Added an answer on June 3, 2026 at 3:08 am

    With pre-C99 C you could initialize only the first member of the union. However, starting with C99 if you provide a name for the union member, you could use C99 feature of designated initializers:

    typedef struct gameitem {
        itemtype type;
        char* name;
        unsigned short bulk;
        unsigned short weight;
        union {
            Car c;
            Bullet b;
            Firearm f;
        }u;
    } Item;
    
    objects[0] = (Item) {gun, "a gun", 500, 5000, .u.f=(Firearm) {handgun, 10}};
    objects[1] = (Item) {car, "a car", 3500, 4000, .u.c=(Car) {200}};
    objects[2] = (Item) {bullet, "a bullet", 200, 3000, .u.b=(Bullet) {300, 5000}};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After implementing some of the solutions in my previous question , I've come up
I'm implementing PayPal IPN & PDT. After some headache & time at the sandbox,
I am hitting this error when implementing core data. I have created a entity
After implementing most of the common and needed functions for my Graph implementation, I
We have a strange problem when implementing sessions with ColdFusion in IE6. After login
I'm having trouble understanding the instructions of implementing Fannkuch. Instructions: http://www.haskell.org/haskellwiki/Shootout/Fannkuch After step Count
I keep running into a pretty frustrating error in Xcode after implementing a date
For a particular piece of homework, I'm implementing a basic data storage system using
I am implementing a simulated b/s stock data system. I am using flex and
i am implementing a core data example in xcode 4.2 at insertMethod (in MasterViewController.m

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.