我正在撰寫一個編譯器,我有這個代碼:
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->next 時,它也會增加 e。為什么會這樣?
uj5u.com熱心網友回復:
您不是在修改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://www.uj5u.com/caozuo/356252.html
