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

The Archive Base Latest Questions

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

I have a quick question..is there any difference in these: struct myinnerstruct { int

  • 0

I have a quick question..is there any difference in these:

struct myinnerstruct
{
    int x;

};

struct mystruct
{
    struct myinnerstruct m;
    int y;
};

AND THIS

struct mystruct
{
    int x;
    struct myinnerstruct
    {
        int y;
    };
    struct myinnerstruct m; 
};

These both work as far as I can tell, but I’m wondering if there’s a reason to pick one or the other. 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-05-25T13:35:54+00:00Added an answer on May 25, 2026 at 1:35 pm

    The difference is that the second one is invalid.

    The stuff between the { and } in a struct declaration is a sequence of member declarations. Your

        struct myinnerstruct
        {
            int y;
        };
    

    is a type declaration; it doesn’t declare a member of the enclosing struct, so it’s illegal in that context.

    What you can do is this:

    struct mystruct
    {
        int x;
        struct myinnerstruct
        {
            int y;
        } m;
    };
    

    The declaration of m is a member declaration, so it’s ok; it also declares the type struct myinnerstruct. But in my opinion, it’s poor style. The type struct myinnerstruct remains visible after the declaration of struct mystruct is completed; see below for the explanation.

    If you really want a struct within a struct like that, and you’re not going to use struct myinnerstruct anywhere else, you could leave it without a tag:

    struct mystruct
    {
        int x;
        struct
        {
            int y;
        } m;
    };
    

    But then you might as well declare y as a member of struct mystruct.

    If you want struct innerstruct to be a named type, just declare it separately, as you did in your first example.

    Here’s the explanation of why struct innerstruct remains visible.

    The C99 standard (large PDF), section 6.2.1 paragraph 2, says:

    For each different entity that an identifier designates, the
    identifier is visible (i.e., can be used) only within a region of
    program text called its scope. Different entities designated by the
    same identifier either have different scopes, or are in different name
    spaces. There are four kinds of scopes: function, file, block, and
    function prototype. (A function prototype is a declaration of a
    function that declares the types of its parameters.)

    The C90 and C11 standards have essentially the same wording.

    The { braces } in a struct declaration do not define a block, nor do they define any of the other possible kinds of scope, so anything declared between the braces is not scoped to that region; it must be scoped to some surrounding context. It happens that the syntax lets you declare struct myinnerstruct inside another struct definition — but only if it’s part of a member definition. I think this is allowed only because the designers of the language didn’t go to any extra effort to disallow it; it’s just a side effect of other rules. You can do it, but I don’t recommend it.

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

Sidebar

Related Questions

Hey just a quick question for any experts out there. I have a site
Hey guys, one more quick question for any experts out there. I have a
Just a quick question. Is there any performance difference between using PDO::fetchAll() and PDO::fetch()
I have quick question about jQuery plugins, hope somebody out there has some advice.
Quick question for the DBA's out there: Say I have 2 columns on my
I have a (hopefully) quick question. I've got some stepper boxes. Though really this
I have a quick question, I have values of day (int), month (String), year
Quick question... Is there any way to change the default behavior of a scrollbar
I have quick question for you SQL gurus. I have existing tables without primary
I have a quick question about fetching results from a weakly typed cursor and

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.