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

 

posted @ 2023-07-18 16:27  依然依然的a  阅读(10)  评论(0编辑  收藏  举报