1

我有一个如图所示的结构。当我有一个指向结构的指针时,我可以正常初始化或修改它的任何成员。

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

但是,当我将结构指针的地址传递给函数并使用双指针捕获它并尝试使用该双指针访问成员时,我的编译器会抛出错误“成员未定义”。

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';
}

而且,这就是我访问该功能的方式。

DFSVisit(&root);

Root 是一个正确初始化的指针。而且,Root 是一个全局变量。

4

2 回答 2

6

*u->color解析为*(u->color)而不是您想要(*u)->color的,因此编译器会抱怨,因为 anode*没有color成员(因为它是指针而不是结构!)。因此,要么显式插入括号,(*u)->color要么引入局部变量:struct node *node = *u;并使用node->color

于 2013-01-12T10:05:38.750 回答
6

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.

于 2013-01-12T10:06:01.490 回答