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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:44:39+00:00 2026-06-02T08:44:39+00:00

I want to do something like this. typedef struct Test{ int value; struct Test*

  • 0

I want to do something like this.

typedef struct Test{
  int value;
  struct Test* parent;
  struct Test** children;
}Test;

So I want a node that points to another parent structure. Then I want a dynamically allocated array that points to child nodes. My question is that I have no idea how this would work syntactically.

For example,

Test* first;
Test* second;
Test* third;
(*third).value = 1;
(*first).parent = second;
(*first).child[0] = third;
printf("%d\n",(*first).(*child[0]).value);

doesn’t compile. I’m assuming I need to do something with malloc to allocate space for the array of pointers but I’m not sure. Also I’m not sure how I would access the “value” of the parent and child directories.

  • 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-02T08:44:41+00:00Added an answer on June 2, 2026 at 8:44 am

    EDIT: I’ve added an ideone link to the end that implements all the concepts for you.

    Sorry for the terseness of this answer, I am hoping it will show you how to do it properly.

    Test* first = (Test *)malloc(sizeof(Test));  // malloc(sizeof(Test)) allocates enough memory to hold a Test struct
    Test* second = (Test *)malloc(sizeof(Test));
    first->value = 1; // -> is the proper way to dereference pointers in this situation (sorry wrong term? I am up late) but I suppose your style can work, it just gets a bit confusing IMO
    first->*child = (Test *)malloc(intptr_t * number_of_children); // intptr_t will make sure you have the right size of a pointer, you could also use sizeof(Test *) instead. i.e. malloc(sizeof(Test *));
    first->child[0] = second; // The array-style subscript is just more readable IMO
    printf("%d\n",first->child[0]->value); // child[0]-> will handle the dereferencing in a nice way
    

    But I’m going to show you a bit of a trick to make your life easier

    typedef Test* test_array;
    
    // ...later, in the struct...
    test_array* child;
    
    // ...later, in the malloc place...
    
    first->child = (test_array *)malloc(sizeof(test_array *) * number_of_children);
    

    Everything else stays the same, you just get much easier to understand syntax IMO. Helps deal with those tricky double stars.

    EDIT: here’s the link – http://ideone.com/TvSSB

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

Sidebar

Related Questions

I have a list in C that is something like this: typedef struct _node
I have something like this: typedef int AnotherType; template <typename T> Func( T Value
x__x I want to do something like this: typedef long (* fp)(BaseWindow< fp >
I want to do something like this: #define EQ4(a_,b_) (*(int*)(a_)==*(int*)(b_)) char *s1 = food;
I have something like this in my code typedef struct ts_fem_mesh { double **vertices;
I have something like this, in fact more complex struct than this: typedef struct
I have a simple structure that is defined like so: typedef struct { int
i want something like this the user enter a website link i need check
I want something like this: <msxsl:script language=C#> ??? getNodes() { ... return ... }
I want something like this: abcdab.search(/a/g) //return [0,4] Is it possible?

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.