无法修改作为参数传递给函数的指针变量

编程入门 行业动态 更新时间:2024-10-28 18:25:32
本文介绍了无法修改作为参数传递给函数的指针变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有此功能

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; }

更多推荐

无法修改作为参数传递给函数的指针变量

本文发布于:2023-09-24 05:07:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   变量   函数   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!