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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:16:55+00:00 2026-06-13T23:16:55+00:00

This is a contrived example, but expresses the core problem. I have a union

  • 0

This is a contrived example, but expresses the core problem. I have a union and a structure with the same nested structure from an existing code base:

typedef union _MyUnion
{
  unsigned int A;
  struct _MyNestedStruct
  {
    int field
  } MyNestedStruct;
} MyUnion;

and

typedef struct _MyStruct
{
  struct _MyNestedStruct
  {
    int field
  } MyNestedStruct;
} MyStruct;

If I compile this under the Green Hills Compiler (GHC), then there are no problems. Compiling under GCC gives the error “error: redefinition of ‘struct _MyStruct'”.

First of all, why was I able to successfully compile with a named structure as a nested structure? I suspect that GHC is compiling with some C++ rules, or it supports named structures in a nested structure.

Second, what would allow me to successfully compile without any significant code changes? I know I can just delete the _MyNestedStruct from the nested structure, but does this have any consequences?

  • 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-13T23:16:56+00:00Added an answer on June 13, 2026 at 11:16 pm

    In C there are no namespace scopes, implicit or explict, so all the structure names share the same name space. Hence, struct _MyNestedStruct is defined twice and it is an error. GCC is correct, GHC is not.

    If it were C++, then the code would be correct, because each struct would create a different namespace scope and so there is no name clash.

    Solutions that I can think of:

    A) If the structures are actually the same, define it just once outside of the other ones (you could have it inside the first one, but that would be weird).

    struct _MyNestedStruct
    {
        int field
    };
    
    typedef union _MyUnion
    {
      unsigned int A;
      struct _MyNestedStruct MyNestedStruct;
    } MyUnion;
    
    typedef struct _MyStruct
    {
      struct _MyNestedStruct MyNestedStruct;
    } MyStruct;
    

    B) Name the two structures differntly. In C the name is not so important, as long the definition is identical.

    C) Use nameless structures. I reckon you will not be using these names at all:

    typedef union _MyUnion
    {
      unsigned int A;
      struct
      {
        int field;
      } MyNestedStruct;
    } MyUnion;
    
    typedef struct _MyStruct
    {
      struct
      {
        int field;
      } MyNestedStruct;
    } MyStruct;
    

    BTW, remember that any name beginning with and underscore plus an uppercase is reserved for the implementation and should not be defined by the user.

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

Sidebar

Related Questions

This is a contrived example, but it illustrates my problem much more concisely then
This is a contrived example of what I want to do, but minimally expresses
This is a contrived example, but lets say I have declared objects: CustomObj fooObj;
If I have a few UNION Statements as a contrived example: SELECT * FROM
I realize this is a very contrived example, but I've simplified the full version
In this very contrived example, I have an array with 3 elements that I'm
Contrived example, but with 'code first' EF4 could I store in the database a
I realize this is a contrived example, but I want a compile check to
It's a horribly contrived example, but anyway... this typechecks: newtype Foo c = Foo
This may seem like a somewhat contrived example, but I'm left scratching my head.

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.