int find(char* a, trie_node* node, int result){//need to make sure a is not NULL at beginning
int i,temp;
if ((a != NULL && a[0] !='\n') && node->children[a[0] - 97] == NULL)
{
result = 0;//not found any children satisfied the requirement
}
else if ((a != NULL && a[0] !='\n') && node->children[a[0] - 97] != NULL){
temp = a[0];
a++;
find(a, node->children[temp - 97], result);
} else{//a == NULL, which means end of the find procedure, just return the num_children
result = node->num_children; //most inner one
}
return result;
}
我試圖從這個函數返回結果。由於它是一個嚴格的c程序,我需要在函數結束時返回聲明。C:從遞歸調用返回一個值
我在gdb中跟蹤它後,最內部的函數調用返回正確的數字結果。但是,在返回外部函數期間結果的值會丟失。因此,這個函數將返回0,這是錯誤的。我怎麼能夠回報並保持最內在的價值?
爲什麼你的函數有'result'參數?它的價值從未被使用過。 – melpomene
@melpomene最初,我沒有在添加功能。然而,因爲我需要返回函數結尾的結果,我認爲最好有一個參數作爲一個全局變量,它可以存儲來自內部函數的值。這就是爲什麼我添加結果。 –
參數是局部變量。 「*作爲全局變量的參數*」沒有意義。 – melpomene