1. 程式人生 > 實用技巧 >005_linuxC++之_指標的引入

005_linuxC++之_指標的引入

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 }
View Code

執行結果