user1386966
user1386966

Reputation: 3392

what is the meaning of a double pointer of struct in C

I have a problem understanding this structure , would love to get a clear explanation.

typedef struct exp{
   int x;
   struct exp *parent;
   struct exp **children;
}

what does it mean the parent and the children ? "parent" is an array of this structure? and what is the meaning of the children ? it's an array of arrays?! I really can't understand..

and last thing, If I'm adding an element , it becomes a specific child of some parent, how can I reach all the children of a parent? shouldn't it be a structure "List" (using next etc .. ? )?

thank you!!

Upvotes: 0

Views: 1563

Answers (5)

John Bode
John Bode

Reputation: 123548

First of all, remember that a structure cannot contain an instance of itself. IOW, you cannot do something like

struct exp {
  int x;
  struct exp parent;
  ...
};

The main reason for this is that the struct type is not complete at the point where you try to declare the parent member; since you haven't finished describing the type yet, the compiler doesn't know how big the parent member should be. Not to mention that the parent member would itself have a parent member of type struct exp, which would in turn have a parent member of type struct exp, which would in turn have a parent member of type struct exp, etc., etc., etc., etc. You'd wind up with an object that required an infinite amount of storage.

So you cannot have a member of type struct exp. However, you can have a pointer to an object of type struct exp; you can create pointers to incomplete types, and all struct pointer types have the same size and representation (IOW, struct foo *pf and struct bar *pb will have the same size, even if struct foo and struct bar do not).

So the parent member is (probably) only meant to point to a single instance of type struct exp (each instance of struct exp has one parent). Likewise the children member is (probably) meant to be a single-dimensioned array of pointers to struct exp (each instance of atruct exp can have zero, one, or more children).

Upvotes: 0

argentage
argentage

Reputation: 2778

It's an array of pointers to child nodes- this looks like some kind of tree structure.

There is a parent node for each node, and each node has one or more children. You can travel from a parent to one of its children with

expInstance->children[i];

Where i is a number indicating one of the child nodes. It's not really clear how many child nodes there are from this definition- it could be one, or two, or a million. But given that information you could loop through them. Either with

for(i=0; i<NUMBER_OF_CHILDREN_NODES;i++){
    expInstance->children[i];
}

if you know the length of the array in advance or the somewhat strange

while(expInstance.children[i++]){
    expInstance->children[i];
}

(There are a couple variously clever ways to do this, but there has to be the assumption built into the structure that there will be a null pointer in the last slot of this array in order to terminate it.)

Upvotes: 1

Mihai Maruseac
Mihai Maruseac

Reputation: 21460

This image shows a possible scenario:

a possible scenario

It was obtained using this code and DDD

#include <stdio.h>
#include <stdlib.h>

struct exp{
    int x;
    struct exp *parent;
    struct exp **children;
};

int main ()
{
    struct exp *x = calloc(1, sizeof(x[0]));
    x->x = 42;
    x->parent = calloc(1, sizeof(x[0]));
    x->children = calloc(5, sizeof(x->children[0]));
    x->children[0] = calloc(1, sizeof(x[0]));
    x->children[2] = calloc(1, sizeof(x[0]));
    x->children[3] = calloc(1, sizeof(x[0]));
    x->children[4] = calloc(1, sizeof(x[0]));
    return 0;
}

Basically, the children field is a vector of pointers to struct exp. You decide how many elements to put there and the other things.

PS: Code is only a demo, it has not quite a good quality.

Upvotes: 4

localhost
localhost

Reputation: 440

It's a pointer to a pointer, which in that case seems to be used as a list of struct exp.

Each struct exp has a reference to it's "parent", and a pointer to a list of children struct exp.

typedef struct exp{
    int x;
    struct exp *parent;
    struct exp **children;
} element;

// create ROOT elemnt
element * root = (element*) malloc(sizeof(element)); //alocate mem. for 1 element

Once we have a "root" we can add children, following is pseudo-code

for 1 to 10{
    child = new element;
    child->parent = root;                // tell the child who is his parent
    addToRoot( root , child);            // call a function that inserts elemnts to root

}

So now we should have root with a list of 10 elements:

_______________                                    _______________   
|             | (children)                         |             | - (parent) points to struct exp, root
|     root    | - points to list of struct exp  -> |   child 0   |   
|             |                                    |             | - (children) points to null; // if it's empty  
_______________                                    _______________ 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root
                                                   |   child 1   |     
                                                   |             | - (children) points to null; // if it's empty 
                                                   _______________ 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root 
                                                   |   child 2   |      
                                                   |             | - (children) points to null; // if it's empty  
                                                   _______________ 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root 
                                                   |   child 3   |      
                                                   |             | - (children) points to null; // if it's empty  
                                                   _______________ 

                                                         .
                                                         .
                                                         . 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root 
                                                   |   child 9   |      
                                                   |             | - (children) points to null; // if it's empty  
                                                   _______________    

Something like that... Did it help?

Upvotes: 3

Sudhir Krishnan
Sudhir Krishnan

Reputation: 281

It is hard to say without seeing more code. Are you trying to implement a tree? From this definition, this is what apparent to me:

  • parent points to the parent node of this structure.
  • children points to the array of pointers of all its child nodes.

Upvotes: 0

Related Questions