我有此功能
int rt_exist(struct route_entry* prev_rte) { prev_rte = rte_head; //This doen't assigns rte_head to prev_rte return 0; }其中 rte_head 是初始化的struct route_entry *指针变量. 但是在上述情况下,未为"prev_rte"分配rte_head的值.
where rte_head is an initialized struct route_entry* pointer variable. But in the above case "prev_rte" is not assigned the value of rte_head.
顺便说一句,如果我做这样的事情
By the way ,I if I do something like this
int rt_exist(struct route_entry* prev_rte) { struct route_entry* rte_new; rte_new = rte_head; //But this can return 0; }上述分配顺利进行.当指针变量作为函数参数传递时会出现问题.
The above assignment occurs smoothly . The problem arises when pointer variable is passes as function argument.
这是一个很奇怪的问题,没有太多细节,但是有人可以指出我可能的方向还是我做错了什么.
It's a weird question with little details ,but can someone point me to a possible direction or something wrong I might be doing.
推荐答案请考虑以下问题:
void foo (int j) { j = 7; } foo (8);您希望在这里发生什么?函数无法以这种方式在调用方中更改值.
What do you expect to happen here? A function can't change a value in the caller this way.
这里应该发生什么:
rt_exist (NULL);对于C ++,您可以使用引用:
For C++, you can use references:
int rt_exist(struct route_entry*& prev_rte) { prev_rte = rte_head; //This doen't assigns rte_head to prev_rte return 0; }对于C,您需要传递一个指向您要更改的对象的指针:
For C, you need to pass a pointer to the thing you want to change:
int rt_exist(struct route_entry** prev_rte_ptr) { *prev_rte_ptr = rte_head; //This doen't assigns rte_head to prev_rte return 0; }更多推荐
无法修改作为参数传递给函数的指针变量
发布评论