c++ - 如何正确地将带有变量地址的指针从函数传递到另一个接受引用的函数
问题描述
我遇到的问题是我在 main 中声明的指针指向从函数返回的变量的地址,后来我尝试在接受引用的函数中传递指针,但是值它现在已经消失并以某种方式被垃圾值所取代。请告诉我我做错了什么。
#include<iostream>
void somthingwrong(int &x) //*takes in the pointers address*
{
std::cout << somfing << std::endl; //*supposed to print 5 but outputs a
// garbage value*
return;
}
int& number()
{
int g = 5;
return g;
}
int main()
{
int *h = nullptr;
h = &number(); //*the pointer passed gets the value of 5*
somthingwrong(*h); //*here in this function the value is somehow erased
//and couts some garbage value*
return (0);
}
解决方案
推荐阅读
- python - 如何从 Julia 程序中调用 Python 函数?
- python - 获取数据框中每个用户发生类别更改的日期
- java - 不存在 [xxxx.xxxx.xxxx] 类型的 bean。确保 bean 没有被 bean 要求 Micronaut 消息传递应用程序禁用
- firebase - 在餐厅评论示例中,我如何找到哪家餐厅
- sql - 如何从表列中循环对象数组?
- javascript - 尝试使用“framer-motion”模块时的Webpack“意外令牌导出”
- api - API 在使用 fetch 挂钩时有效。使用 @click 方法时,相同的 API 不起作用。纽斯特
- reactjs - 多个组件上的 setInterval 导致不匹配的动画
- laravel - 这是我的代码,我想将图像 url 保存在数据库中我正在使用 laravel 我的问题是我如何将图像 url 保存在数据库中
- php - PHP 正则表达式替换 Html 结束标签