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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:57:13+00:00 2026-06-04T02:57:13+00:00

So, say I have the following C++ header, testheader.h : struct mystruct { struct

  • 0

So, say I have the following C++ header, testheader.h:

struct mystruct
{
  struct myinnerstruct
  {
        int x;
  } astruct;
};

struct myinnerstruct
{
    int x;
};

and the following C++ source, test.cpp:

#include "testheader.h"
using namespace std;
int main()
{
    return 0;
}

g++ gives no issues during compile/link.

Now, if I have the same header, but instead of the C++ source, a C source file test.c:

#include "testheader.h"
int main()
{
  return 0;
}

And I compile with gcc, I get the following error:

error: redefinition of struct myinnerstruct

So, I gather that the scope of the C version is the translation unit, and the C++ version is block scoped? Can someone confirm this is the case, and maybe give me a reason of why it makes sense? I’m doing some mixing of C and C++ code, and this is giving me quite a bit of trouble.

Any insight is greatly appreciated. Thanks!

  • 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-04T02:57:15+00:00Added an answer on June 4, 2026 at 2:57 am

    In C nested structs do not actually live in their parents scope. However in C++ they do- teststruct::innerstruct is a different type to innerstruct. This is to improve encapsulation in C++ code. Without this rule, no two types in the same namespace could define a nested iterator class, for example, which would be extremely bad.

    C treats structs in plenty of other very silly ways, so it’s no surprise that they did the wrong thing here. However, C does not otherwise allow type scoping and having a sane rule here would have introduced many additional concepts to the language- ultimately, it would have required the introduction of namespaces, which for some reason was never done.

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

Sidebar

Related Questions

Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
Say I have the following DIVs: <div id=top>Page header</div> <div id=main>Main content</div> <div id=sidebar>Sidebar
Let's say I have the following HTML: <table id=foo> <th class=sortasc>Header</th> </table> <table id=bar>
Lets say I have the following code: <html> <body> <header> <h1> Hello World </h1>
Static variable has file scope. Say I have two following files: file1.h file1.cpp file2.h
Let's say i have a header file Snake.h: #include SnakeBodyPart.h #include GUI.h //... And
let's say I have the following loops: for(int i=left; i < right; i++) {
Say I have the following CSS and HTML code: #header { height: 150px; }
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Say I have the following: namespace SomeProject { public interface ISomething { string SomeMethod();

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.