我正在编程一个编译器,我有以下代码:
CLOSURE *find_func(TOKEN* name, FRAME* e){
FRAME *ef = e;
while(ef != NULL){
while (ef->bindings != NULL){
if(ef->bindings->name == name){
return ef->bindings->value->closure;
}
ef->bindings = ef->bindings->next;
}
ef = ef->next;
}
printf("No function %s in scope, exiting...n",name->lexeme);exit(1);
}
我的理解是,当我将e复制到ef中,然后用ef执行循环时,它不会更改存储在e中的地址?然而,该代码确实如此;当我转到ef=ef->接下来,它还增加了e。为什么会发生这种情况?
您不是在修改e
,而是在修改分配ef->bindings
时它所指向的帧。
因此,使用一个新的变量来代替结构成员。
CLOSURE *find_func(TOKEN* name, FRAME* e){
FRAME *ef = e;
while(ef != NULL){
BINDINGS *ef_bindings = ef->bindings;
while (ef_bindings != NULL){
if(ef_bindings->name == name){
return ef_bindings->value->closure;
}
ef_bindings = ef_bindings->next;
}
ef = ef->next;
}
printf("No function %s in scope, exiting...n",name->lexeme);
exit(1);
}