我正在编写一个编译器,我有这样的代码:
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。为什么会发生这种情况?
发布于 2021-11-09 18:12:53
您不是在修改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);
}https://stackoverflow.com/questions/69902866
复制相似问题