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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:16:10+00:00 2026-06-07T02:16:10+00:00

Could someone please explain to me whats wrong with this code? #include<stdio.h> struct A{

  • 0

Could someone please explain to me whats wrong with this code?

#include<stdio.h>

struct A{
    int i;
    struct A* parent; 
    struct B test; // error: field ‘test’ has incomplete type
};
struct B{
    struct A* rootParent;
    int ref;
    int something;
};



int main(){
    struct A some, some2;
    some.i = 0;
    some.parent = &some2;
    some.test.rootParent = &some;
    some.test.ref = some.test.something = 0;
    some2.i =0; 
    some2.parent = 0;
    some2.test.rootParent = 0;
    some2.test.ref = some2.test.something = 0;
    return 0;
}

It seems I’m missing something basic here. Why does the order of A and B matter?
Is it possible to make it so it will not matter?

If I change the order of deceleration everything works, B first.

  • 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-07T02:16:11+00:00Added an answer on June 7, 2026 at 2:16 am

    cnicutar is almost right. Here’s an explanation and the correct answer which is to reorder the declarations.

    The compiler needs to calculate the size of a struct when it reads the definition. In your case, the size of struct B is unknown so you are getting an error. So you can swap the order of declaration in this simple case:

    struct B{
        struct A* rootParent;
        int ref;
        int something;
    };
    
    struct A{
        int i;
        struct A* parent; 
        struct B test;
    };
    

    Which will work even though struct A is an unknown type. It only works because the reference to the type is a pointer so the size is known. If you think that you need two types that contain full instances of each other, then you cannot use this method. In fact, you cannot declare a struct A that contains a struct B by-value which contains a struct A member by-value. It turns out that it is rather non-sensical anyway – one of the links is required to be a reference.

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

Sidebar

Related Questions

Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
Could someone please explain what this does and how it is legal C code?
Could someone please explain this weird phenomenon: http://jsfiddle.net/sPA2V/ For some reason, the box footer
Could someone please explain the difference between the following two lines of code: 1.
Could someone please help explain why I can't get this to work? I properly
Could someone please explain what an I/O Error 32 refers to in the context
Could someone please explain what the for loop in this class does? Specifically the
Could someone please explain to me what the following lines of code do? dynamic
Could someone please explain the purpose of this option and what exactly it is
could someone please explain why the first unit test class works whereas the second

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.