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

  • Home
  • SEARCH
  • 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 497019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:44:30+00:00 2026-05-13T05:44:30+00:00

I have a struct: typedef struct _n { int type; union { char *s;

  • 0

I have a struct:

typedef struct _n
{
    int type;
    union {
        char *s;
        int i;
    };
} n;

When I try to assign a compound literal, like:

node n1 = {1, 0};
node n2 = {2, "test"};

gcc gives me some warnings such as:

warning: initialization makes pointer from integer without a cast
warning: initialization from incompatible pointer type

Well, it’s clear that the compiler isn’t sure about me just assigning a value to a possibly ambiguous type. However, even if I try to specify more precisely:

node n0 = {type: 1, i: 4};

I get:

error: unknown field ‘i’ specified in initializer

I have read that if I put (union <union name>) before i: then it may work. However, I prefer having an anonymous union. Is there a way to do it?

  • 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-13T05:44:30+00:00Added an answer on May 13, 2026 at 5:44 am

    Anonymous unions are not standard C — they are a compiler extension. I would strongly recommend giving the union a name. If you insist on using an anonymous union, then you can only give an initializer for the first element of it:

    node n0 = {1, 0};  // initializes `s' to 0
    

    When compiled with -Wall -Wextra -pedantic, gcc gave me the warning "missing braces around initializer", which is a valid warning. The initializer should actually be specified like this:

    node n0 = {1, {0}};  // initializes `s' to 0
    

    Now this only gives a warning that "ISO C doesn’t support unnamed structs/unions".

    If you give the union a name, then you can use a C99 feature called designated initializers to initialize a specific member of the union:

    typedef struct
    {
        int type;
        union {
            char *s;
            int i;
        } value;
    } node;
    
    node n0 = {1, { .s = "string" }};
    node n1 = {2, { .i = 123 }};
    

    You need the union to have a name; otherwise, the compiler will complain about the designated initializer inside it.

    The syntax you were trying to use node n0 = {type: 1, i: 4} is invalid syntax; it looks like you were trying to use designated initializers.

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

Sidebar

Related Questions

I have a struct like so typedef struct person { int id; char name[20];
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
Let's say I have a first structure like this: typedef struct { int ivalue;
I have a structure typedef struct store { char name[11]; int age; } store;
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have a struct like this typedef struct bookStruct { char title[80]; char author[80];
I have a struct like this typedef struct _somestruct { int a; int b;
I have a type defined like this: struct DynTab_s { int object_count; //other fields
Say I have a struct like the following ... typedef struct { int WheelCount;
I have a struct defined as typedef struct { char* name; char* ID; int

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.