[code language=”cpp”]
#include <iostream>
using namespace std;
void a(int* n); //引數為指標的函數宣告
int main(){
}<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]
[code language=”cpp”]
#include <iostream>
using namespace std;
void a(int*); //引數為指標的函數宣告
//可以省略引數名稱
int main(){
}<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]
[code language=”cpp”]
#include <iostream>
using namespace std;
void a(int*);
int main(){
}
void a(int* n){ //引數為指標的函數定義
cout << *n << endl;
}
[/code]
[code language=”cpp”]
#include <iostream>
using namespace std;
void a(int*);
int main(){
int k=10; //宣告整數型態變數k
int* m=&k; //宣告整數型態指標m,並將它指向k
a(&k); //呼叫函數a,並傳入k的位址
a(m); //呼叫函數a,並傳入指標m
}
void a(int* n){
cout << *n << endl;
}
[/code]
[code language=”cpp”]
#include <iostream>
using namespace std;
void change(int*,int*);
int main(){
int m=10,n=15;
int* p1=&m;
int* p2=&n;
change(p1,p2);
cout << "m=" << m << endl;
cout << "n=" << n << endl;
}
void change(int* a,int* b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
[/code]
[code language=”cpp”]
#include <iostream>
using namespace std;
int* large(int*,int*); //引數以及傳回值皆是指標
int main(){
int m=10,n=15;
int* p1=&m;
int* p2=&n;
int* p3;
p3=large(p1,p2); //指標p3接收函數的傳回值
cout << *p3 << endl; //印出指標p3所指向的內容 } int* large(int* a,int* b){ if(*a>*b)
return a;
else
return b;
}<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]