c++ 参数引用传递
1 #include <iostream> 2 #include <thread> 3 #include<windows.h> 4 using namespace std; 5 void A(int& a) { 6 cout <<"address" << &a << endl;; 7 cout << "value" << a << endl;; 8 a = 100; 9 10 } 11 int main() { 12 int b = 10; 13 cout << "address b is :" << &b << endl;; 14 cout << "********"<<endl; 15 A(b); 16 cout << b; 17 18 return 0; 19 }
要是直接在参数中传入指针可不可以呢,下面是示例代码,是不可以的
#include <iostream> #include <thread> #include<windows.h> using namespace std; void A(int& a) { cout << "address" << &a << endl;; cout << "value" << a << endl;; a = 100; } int main() { int b = 10; int* c = &b; cout << "address b is :" << &b << endl;; cout << "********" << endl; A(b); cout << b; //A(c);//这样会报错 A(*c);//这样就不会 return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通