[英]Change the reference of a struct in C
我有一段時間執行一些計算,我必須在某些節點中進行迭代。
我的結構是:
typedef struct {
int pointer;
int *n; //an array
} ENTRY;
typedef struct {
int nofentries; //number of entries
RENTRY *entries; //array of entries
} NODE;
我的功能是:
NODE *choose_node(NODE *currentnode, ENTRY *input) {
NODE *n;
//it copies a node by using memcpy (including its struct fields)...
n = node_clone(currentnode);
while(true) {
//my computation to choose a entry....
if(my computation is true)
return n;
//my doubt is here:
//I have a stack that stores the visited elements
//the push function does NOT copy the NODE
//it aims only to store the references
stack_push(stack, n);
//then we update the node for the next level
//this function gets other node (it returns a pointer of a new NODE)
n = get_node(n->entries[entry]->pointer);
}
}
如果我更改n指向的位置,我的堆棧是否能夠正確存儲節點引用? 我害怕丟失訪問節點的引用。
然后,如果我從堆棧中彈出節點,結果會是預期的嗎?
我在這里會遇到哪些問題?
堆棧應該保留您對舊指針的引用,因此在 n 對它的引用上放置一個新指針不會丟失堆棧上的引用。 似乎它會按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.