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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:51:07+00:00 2026-06-18T15:51:07+00:00

How can I have struct that contains a type of itself. struct node {

  • 0

How can I have struct that contains a type of itself.

struct node { struct node *nodes[MAX]; int ID; };

struct node *node1, *node2;
node1 = (struct node*) malloc(sizeof(struct node));
node2 = (struct node*) malloc(sizeof(struct node));
node1->ID = 1;
node2->ID = 2;
node1->nodes[0] = node2;
node2->nodes[0] = node1;

There are no errors but the program doesn’t execute correctly.

EDIT: I’ve added more of my code.

FINAL: It was a mistake in my part that I created an infinite recursion. I’ll proceed to delete this threat. Sorry for your time spent.

  • 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-18T15:51:09+00:00Added an answer on June 18, 2026 at 3:51 pm

    That’s because you are storing an array of pointers to the struct. That’s quite different.

    You can’t have the same struct inside itself. That would be an infinitely recursive definition.

    Now, if you would show more of your program we may be able to help you understand why your program doesn’t run the way you expect. Chances are you have not initialised the pointers because of confusion over exactly what they are.

    [edit] Now that you have posted some code, and ignoring that you haven’t said exactly what is going wrong, I expect that you are trying to iterate over the entire pointer list while examining your graph, but you never initialised it.

    When you malloc, the memory will be uninitialised. Standard practice in C is to use calloc instead which will set all bytes to zero. Since you seem to be using the nodes array as a list, you may want to add a num_edges field to the node and make a function to do a two-way join on two nodes.

    struct node {
        int num_edges;
        struct node *nodes[MAX];
    };
    
    int join( struct node *a, struct node *b )
    {
        if( a->num_edges >= MAX || b->num_edges >= MAX ) return 0;
        a->nodes[a->num_edges++] = b;
        b->nodes[b->num_edges++] = a;
        return 1;
    }
    

    You could also test whether there is an edge from a to b like this:

    int has_edge( struct node *a, struct node *b )
    {
        int i;
        for( i = 0; i < a->num_edges; i++ ) {
            if( a->nodes[i] == b ) return 1;
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct that contains three lists and an integer as can be
I have a struct-array that contains details of different reports that can be run.
I have a Delphi DLL that contains the following types: type TStepModeType = (smSingle,
I have a legacy schema that contains tables with primary keys of type binary(16)
I have a struct that looks like this, struct Foo { int a; };
How can we access variables of a structure? I have a struct: typedef struct
wonder whether someone can help me with the following one... I have a struct
I have a function which takes a custom struct as the argument. how can
I have a button that can have a focus css class associated with it
I need to create a completely generic linked list that can contain any type

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.