[英]Change the reference of a struct in C
I have a while that performs some calculation and I have to iterate in some nodes.我有一段时间执行一些计算,我必须在某些节点中进行迭代。
My structs are:我的结构是:
typedef struct {
int pointer;
int *n; //an array
} ENTRY;
typedef struct {
int nofentries; //number of entries
RENTRY *entries; //array of entries
} NODE;
My function is:我的功能是:
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);
}
}
Is my stack able to store correctly the node references if I change where n is pointing?如果我更改n指向的位置,我的堆栈是否能够正确存储节点引用? My afraid is to lost the reference of visited nodes.
我害怕丢失访问节点的引用。
Then, if I pop the nodes from my stack, will the result be expected?然后,如果我从堆栈中弹出节点,结果会是预期的吗?
Which problems I will have here?我在这里会遇到哪些问题?
The stack should keep your reference to the old pointer, so slapping a new pointer over n's reference to it won't lose the reference on the stack.堆栈应该保留您对旧指针的引用,因此在 n 对它的引用上放置一个新指针不会丢失堆栈上的引用。 Seems like it will work as expected.
似乎它会按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.