Krishna
Krishna

Reputation: 425

Pointer to a Structure pointer

I have a structure as shown. And I am able to initialise or modify any of its members normally when I have a pointer to the structure.

struct node{
    int key;
    int nno;
    char color;
    struct node* out;
    struct node* next;
    struct node* pre;
};

But, When I pass an address of the structure pointer to a function and capture the same using a double pointer, and trying to access the members using that double pointer, my compiler is throwing an error 'member undefined'.

void DFSVisit(struct node** u){
    *u->color = 'g';
    struct node* v;
    while(*u->out != NULL){
            v = *u->out;
                    if(v->color == 'w'){
                            v->pre = *u;
                            DFSVisit(&v);
                    }
    } 
    *u->color = 'b';
}

And, this is how I accessed the function.

DFSVisit(&root);

Root is a pointer properly initialized. And also, Root is a global variable.

Upvotes: 1

Views: 282

Answers (2)

Andrew Cheong
Andrew Cheong

Reputation: 30273

Are you aware that the indirection (dereferencing) operator, *, has lesser precedence than the element selection operator, ->? That is, you should be writing (*u)->color, not *u->color.

Upvotes: 6

Paul Hankin
Paul Hankin

Reputation: 58221

*u->color parses as *(u->color) rather than your desired (*u)->color so the compiler complains because a node* has no color member (because it's a pointer rather than a struct!). So either explicitly insert the brackets like (*u)->color or introduce a local variable: struct node *node = *u; and use node->color

Upvotes: 6

Related Questions