005_linuxC++之_指针的引入
(一)直接看代码
1 #include <iostream> 2 using namespace std; 3 int add(int a){ 4 a = a + 1; 5 return a; 6 } 7 int add(int *a){ 8 *a = *a + 1; 9 return *a; 10 } 11 int add_ref(int &b){ 12 b = b + 1; 13 return b; 14 } 15 int main(int arg,char ** argv){ 16 int a = 99; 17 int &c = a; /*使用符号&来进行引用,引用之后c++就相当于a地址中的数进行a++*/ 18 cout<<add(a)<<endl; /*传入的是参数*/ /*a = 100*/ 19 cout<<"a = "<<a<<endl; /*a = 99*/ 20 21 cout<<add(&a)<<endl; /*传入的地址*/ /*a = 100*/ 22 cout<<"a = "<<a<<endl; /*a = 100*/ 23 24 cout<<add_ref(a)<<endl; /*引用*/ /*a = 101*/ 25 cout<<"a = "<<a<<endl; /*a = 101*/ 26 27 c++; /*a = 102*/ 28 cout<<"a = "<<a<<endl; /*a = 102*/ 29 cout<<"c = "<<c<<endl; /*a = 102*/ 30 return 0; 31 }
运行结果