例6.1 通過指針變量訪問整型變量。
#include <iostream>
usingnamespacestd;
intmain( )
{inta,b; //定義整型變量a,b
int*pointer_1,*pointer_2; //定義指針變量*pointer_1,*pointer_2
a=100;b=10; //對a,b賦值
pointer_1=&a; //把變量a的地址賦給pointer_1
pointer_2=&b; //把變量a的地址賦給pointer_2
cout<<a<<" "<<b<<endl; //輸出a和b的值
cout<<*pointer_1<<" "<<*pointer_2<<endl; //輸出*pointer_1和*pointer_2的值
return0;
}
例6.10 定義一個字符數(shù)組并初始化,然后輸出其中的字符串。
#include <iostream>
usingnamespacestd;
intmain( )
{ charstr[]="I love CHINA!";
cout<<str<<endl;
return0;
}
例6.11 定義一個字符串變量并初始化,然后輸出其中的字符串。
#include <string>
#include <iostream>
usingnamespacestd;
intmain( )
{ string str="I love CHINA!";
cout<<str<<endl;
return0;
}
例6.12 定義一個字符指針變量并初始化,然后輸出它指向的字符串。
#include <iostream>
usingnamespacestd;
intmain( )
{ char*str="I love CHINA!";
cout<<str<<endl;
return0;
}
例6.13 將字符串str1復(fù)制為字符串str2。
#include <iostream>
usingnamespacestd;
intmain( )
{ charstr1[]="I love CHINA!",str2[20],*p1,*p2;
p1=str1;p2=str2;
for(;*p1!='\\0';p1++,p2++)
*p2=*p1;
*p2='\\0';
p1=str1;p2=str2;
cout<<"str1 is: "<<p1<<endl;
cout<<"str2 is: "<<p2<<endl;
return0;
}
例6.14 求a和b中的大者。
先按一般方法寫程序:
#include <iostream>
usingnamespacestd;
intmain( )
{intmax(intx,inty); //函數(shù)聲明
inta,b,m;
cin>>a>>b;
m=max(a,b); //調(diào)用函數(shù)max,求出最大值,賦給m
cout<<"max="<<m<<endl;
return0;
}
intmax(intx,inty)
{intz;
if(x>y) z=x;
elsez=y;
return(z);
}
現(xiàn)在將上面程序的主函數(shù)修改如下:
#include <iostream>
usingnamespacestd;
intmain( )
{intmax(intx,inty); //函數(shù)聲明
int(*p)(int,int); //定義指向函數(shù)的指針變量p
inta,b,m;
p=max; //使p指向函數(shù)max
cin>>a>>b;
m=p(a,b);
cout<<"max="<<m<<endl;
return0;
}
例6.15 若干字符串按字母順序(由小到大)輸出。
#include <iostream>
usingnamespacestd;
intmain( )
{ voidsort(char*name[],intn); //聲明函數(shù)
voidprint(char*name[],intn); //聲明函數(shù)
char*name[]={"BASIC","FORTRAN","C++","Pascal","COBOL"}; //定義指針數(shù)組
intn=5;
sort(name,n);
print(name,n);
return0;
}
voidsort(char*name[],intn)
{ char*temp;
inti,j,k;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0) k=j;
if(k!=i)
{ temp=name[i];name[i]=name[k];name[k]=temp;}
}
}
voidprint(char*name[],intn)
{ inti;
for(i=0;i<n;i++)
cout<<name[i]<<endl;
}
例6.16 指向字符型數(shù)據(jù)的指針變量。
#include <iostream>
usingnamespacestd;
intmain( )
{ char**p; //定義指向字符指針數(shù)據(jù)的指針變量p
char*name[]={"BASIC","FORTRAN","C++","Pascal","COBOL"};
p=name+2; //見圖6.23中p的指向
cout<<*p<<endl; //輸出name[2]指向的字符串
cout<<**p<<endl; //輸出name[2]指向的字符串中的第一個字符
}
例6.17 引用和變量的關(guān)系。
#include <iostream>
#include <iomanip>
usingnamespacestd;
intmain( )
{ inta=10;
int&b=a; //聲明b是a的引用
a=a*a; //a的值變化了,b的值也應(yīng)一起變化
cout<<a<<setw(6)<<b<<endl;
b=b/5; //b的值變化了,a的值也應(yīng)一起變化
cout<<b<<setw(6)<<a<<endl;
return0;
}
例6.18 要求將變量i和j的值互換。下面的程序無法實現(xiàn)此要求。
#include <iostream>
usingnamespacestd;
intmain( )
{ voidswap(int,int); //函數(shù)聲明
inti=3,j=5;
swap(i,j); //調(diào)用函數(shù)swap
cout<<i<<" "<<j<<endl; //i和j的值未互換
return0;
}
voidswap(inta,intb) //企圖通過形參a和b的值互換,實現(xiàn)實參i和j的值互換
{ inttemp;
temp=a; //以下3行用來實現(xiàn)a和b的值互換
a=b;
b=temp;
}
例6.19 使用指針變量作形參,實現(xiàn)兩個變量的值互換。
#include <iostream>
usingnamespacestd;
intmain( )
{ voidswap(int*,int*);
inti=3,j=5;
swap(&i,&j); //實參是變量的地址
cout<<i<<" "<<j<<endl; //i和j的值已互換
return0;
}
voidswap(int*p1,int*p2) //形參是指針變量
{ inttemp;
temp=*p1; //以下3行用來實現(xiàn)i和j的值互換
*p1=*p2;
*p2=temp;
}
例6.2 輸入a和b兩個整數(shù),按先大后小的順序輸出a和b(用指針變量處理)。
#include <iostream>
usingnamespacestd;
intmain( )
{
int*p1,*p2,*p,a,b;
cin>>a>>b; //輸入兩個整數(shù)
p1=&a; //使p1指向a
p2=&b; //使p2指向b
if(a<b) //如果a<b就使p1與p2的值交換
{p=p1;p1=p2;p2=p;} //將p1的指向與p2的指向交換
cout<<"a="<<a<<" b="<<b<<endl;
cout<<"max="<<*p1<<" min="<<*p2<<endl;
return0;
}
例6.20 利用“引用形參”實現(xiàn)兩個變量的值互換。
#include <iostream>
usingnamespacestd;
intmain( )
{ voidswap(int&,int&);
inti=3,j=5;
swap(i,j);
cout<<"i="<<i<<" "<<"j="<<j<<endl;
return0;
}
voidswap(int&a,int&b) //形參是引用類型
{ inttemp;
temp=a;
a=b;
b=temp;
}
例6.3 題目同例6.2,即對輸入的兩個整數(shù)按大小順序輸出。
這里用函數(shù)處理,而且用指針類型的數(shù)據(jù)作函數(shù)參數(shù)。
程序如下:
#include <iostream>
usingnamespacestd;
intmain( )
{ voidswap(int*p1,int*p2); //函數(shù)聲明
int*pointer_1,*pointer_2,a,b; //定義指針變量pointer_1,pointer_2,整型變量a,b
cin>>a>>b;
pointer_1=&a; //使pointer_1指向a
pointer_2=&b; //使pointer_2指向b
if(a<b) swap(pointer_1,pointer_2); //如果a<b,使*pointer_1和*pointer_2互換
cout<<"max="<<a<<" min="<<b<<endl;//a已是大數(shù),b是小數(shù)
return0;
}
voidswap(int*p1,int*p2) //函數(shù)的作用是將*p1的值與*p2的值交換
{ inttemp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
例6.4 輸入a,b,c 3個整數(shù),按由大到小的順序輸出。
用上面介紹的方法,用3個指針變量指向3個整型變量,然后用swap函數(shù)來實現(xiàn)互換3個整型變量的值。
程序如下:
#include <iostream>
usingnamespacestd;
intmain( )
{ voidexchange(int*,int*,int*); //對exchange函數(shù)的聲明
inta,b,c,*p1,*p2,*p3;
cin>>a>>b>>c; //輸入3個整數(shù)
p1=&a;p2=&b;p3=&c; //指向3個整型變量
exchange(p1,p2,p3); //交換p1,p2,p3指向的3個整型變量的值
cout<<a<<" "<<b<<" "<<c<<endl; //按由大到小的順序輸出3個整數(shù)
}
voidexchange(int*q1,int*q2,int*q3)
{voidswap(int*,int*); //對swap函數(shù)的聲明
if(*q1<*q2) swap(q1,q2); //調(diào)用swap,將q1與q2所指向的變量的值互換
if(*q1<*q3) swap(q1,q3); //調(diào)用swap,將q1與q3所指向的變量的值互換
if(*q2<*q3) swap(q2,q3); //調(diào)用swap,將q2與q3所指向的變量的值互換
}
voidswap(int*pt1,int*pt2) //將pt1與pt2所指向的變量的值互換
{inttemp;
temp=*pt1;
*pt1=*pt2;
*pt2=temp;
}
例6.5 輸出數(shù)組中的全部元素。
假設(shè)有一個整型數(shù)組a,有10個元素。要輸出各元素的值有3種方法:
(1)下標(biāo)法
#include <iostream>
usingnamespacestd;
intmain( )
{ inta[10];
inti;
for(i=0;i<10;i++)
cin>>a[i]; //引用數(shù)組元素a[i]
cout<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" "; //引用數(shù)組元素a[i]
cout<<endl;
return0;
}
運行情況如下:
9876543210↙ (輸入10個元素的值)
9876543210 (輸出10個元素的值)
(2) 指針法
將上面程序第7行和第10行的“a[i]”改為“*(a+i)”,運行情況與(1)相同。
(3) 用指針變量指向數(shù)組元素
#include <iostream>
usingnamespacestd;
intmain( )
{ inta[10];
inti,*p=a; //指針變量p指向數(shù)組a的首元素a[0]
for(i=0;i<10;i++)
cin>>*(p+i); //輸入a[0]~a[9]共10個元素
cout<<endl;
for(p=a;p<(a+10);p++)
cout<<*p<<" "; //p先后指向a[0]~a[9]
cout<<endl;
return0;
}
運行情況與前相同。請仔細(xì)分析p值的變化和*p的值。
例6.6 將10個整數(shù)按由小到大的順序排列。
在例5.7程序的基礎(chǔ)上,將形參改為指針變量。
#include <iostream>
usingnamespacestd;
intmain( )
{voidselect_sort(int*p,intn); //函數(shù)聲明
inta[10],i;
cout<<"enter the originl array:"<<endl;
for(i=0;i<10;i++) //輸入10個數(shù)
cin>>a[i];
cout<<endl;
select_sort(a,10); //函數(shù)調(diào)用,數(shù)組名作實參
cout<<"the sorted array:"<<endl;
for(i=0;i<10;i++) //輸出10個已排好序的數(shù)
cout<<a[i]<<" ";
cout<<endl;
return0;
}voidselect_sort(int*p,intn) //用指針變量作形參
{inti,j,k,t;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(*(p+j)<*(p+k)) k=j; //用指針法訪問數(shù)組元素
t=*(p+k);*(p+k)=*(p+i);*(p+i)=t;
}
}
例6.7 輸出二維數(shù)組各元素的值。
這里采用的方法是用基類型為整型的指針變量先后指向各元素,逐個輸出它們的值。
#include <iostream>
usingnamespacestd;
intmain( )
{ inta[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};
int*p; //p是基類型為整型的指針變量
for(p=a[0];p<a[0]+12;p++)
cout<<*p<<" ";
cout<<endl;
return0;
}
例6.8 輸出二維數(shù)組任一行任一列元素的值。
#include <iostream>
usingnamespacestd;
intmain( )
{ inta[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};
int(*p)[4],i,j;
cin>>i>>j;
p=a;
cout<<*(*(p+i)+j)<<endl;
return0;
}
例6.9 輸出二維數(shù)組各元素的值。
題目與例6.7相同,但本題用一個函數(shù)實現(xiàn)輸出,用多維數(shù)組名作函數(shù)參數(shù)。
#include <iostream>
usingnamespacestd;
intmain( )
{ voidoutput(int(*p)[4]); //函數(shù)聲明
inta[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};
output(a); //多維數(shù)組名作函數(shù)參數(shù)
return0;
}
voidoutput(int(*p)[4]) //形參是指向一維數(shù)組的指針變量
{ inti,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
cout<<*(*(p+i)+j)<<" ";
cout<<endl;
}
