title: Implement N-ary Tree in c
body: <p>I'm trying to implement a N-ary tree in C. I know how to implement if I decide how many childs would have earlier. But my problem is, i want to make it dynamic (i want to resize the children and child size)</p>
  
  <pre><code>struct node    
  {
      char * type;
      struct node **child;
      int children;
      int id;    
  };
  </code></pre>
  
  <p>this is my struct and i want to do that with a pointer of pointer.
  How can i implement the insertion function?</p>
  
  <pre><code>        struct node **toKeep = (*root)-&gt;child;
          (*root)-&gt;children+=1;
          (*root)-&gt;child =malloc(sizeof(struct node*)*
          (*root)-&gt;child = toKeep;
          size_t i; 
          for (i= 0; i &lt; (*root)-&gt;children; i++)
          {
              if ((*root)-&gt;child[i] == NULL)
              {
                  (*root)-&gt;child[i] = newNode(type, id);
                  printf("%d \n", (*root)-&gt;child[i]-&gt;id);
                  break;
              }
          }
  </code></pre>
  
  <p>i tried this to keep existing nodes but im unable to add more than 2 nodes ? What can cause the problem or is there a better way to write it?</p>