C程序设计(第四版)谭浩强_课后习题答案第8章第8章善于利用指针2208.1指针是什么2208.2指针变量2228.2.1使用指针变量的例子2228.2.2怎样定义指针变量2238.2.3怎样引用指针变量2248.2.4指针变量作为函数参数2268.3通过指针引用数组2308.3.1数组元素的指针2308.3.2在引用数组元素时指针的运算2318.3.3通过指针引用数组元素2338.3.4用数组名作函数参数2378.3.5通过指针引用多维数组2458.4通过指针引用字符串2558.4.1字符串的引用方式2558.4.2字符指针作函数参数2598.4.3使用字符指针变量和字符数组的比较2638.5指向函数的指针2668.5.1什么是函数指针2668.5.2用函数指针变量调用函数2668.5.3怎样定义和使用指向函数的指针变量2688.5.4用指向函数的指针作函数参数2708.6返回指针值的函数2748.7指针数组和多重指针2778.7.1什么是指针数组2778.7.2指向指针数据的指针2808.7.3指针数组作main函数的形参2828.8动态内存分配与指向它的指针变量2858.8.1什么是内存的动态分配2858.8.2怎样建立内存的动态分配2858.8.3void指针类型2878.9有关指针的小结288习题2918-1#includestdio.hintmain(){voidswap(int*p1,int*p2);intn1,n2,n3;int*p1,*p2,*p3;printf(inputthreeintegern1,n2,n3:);scanf(%d,%d,%d,&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1n2)swap(p1,p2);if(n1n3)swap(p1,p3);if(n2n3)swap(p2,p3);printf(Now,theorderis:%d,%d,%d\n,n1,n2,n3);return0;}voidswap(int*p1,int*p2){intp;p=*p1;*p1=*p2;*p2=p;}#includestdio.h#includestring.hintmain(){voidswap(char*,char*);charstr1[20],str2[20],str3[20];printf(inputthreeline:\n);gets(str1);gets(str2);gets(str3);if(strcmp(str1,str2)0)swap(str1,str2);if(strcmp(str1,str3)0)swap(str1,str3);if(strcmp(str2,str3)0)swap(str2,str3);printf(Now,theorderis:\n);printf(%s\n%s\n%s\n,str1,str2,str3);return0;}voidswap(char*p1,char*p2){charp[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);}8-3#includestdio.hintmain(){voidinput(int*);voidmax_min_value(int*);voidoutput(int*);intnumber[10];input(number);max_min_value(number);output(number);return0;}voidinput(int*number){inti;printf(input10numbers:);for(i=0;i10;i++)scanf(%d,&number[i]);}voidmax_min_value(int*number){int*max,*min,*p,temp;max=min=number;for(p=number+1;pnumber+10;p++)if(*p*max)max=p;elseif(*p*min)min=p;temp=number[0];number[0]=*min;*min=temp;if(max==number)max=min;temp=number[9];number[9]=*max;*max=temp;}voidoutput(int*number){int*p;printf(Now,theyare:);for(p=number;pnumber+10;p++)printf(%d,*p);printf(\n);}8-4#includestdio.hintmain(){voidmove(int[20],int,int);intnumber[20],n,m,i;printf(howmanynumbers?);scanf(%d,&n);printf(input%dnumbers:\n,n);for(i=0;in;i++)scanf(%d,&number[i]);printf(howmanyplaceyouwantmove?);scanf(%d,&m);move(number,n,m);printf(Now,theyare:\n);for(i=0;in;i++)printf(%d,number[i]);printf(\n);return0;}voidmove(intarray[20],intn,intm){int*p,array_end;array_end=*(array+n-1);for(p=array+n-1;parray;p--)*p=*(p-1);*array=array_end;m--;if(m0)move(array,n,m);}8-5#includestdio.hintmain(){inti,k,m,n,num[50],*p;printf(\ninputnumberofperson:n=);scanf(%d,&n);p=num;for(i=0;in;i++)*(p+i)=i+1;i=0;k=0;m=0;while(mn-1){if(*(p+i)!=0)k++;if(k==3){*(p+i)=0;k=0;m++;}i++;if(i==n)i=0;}while(*p==0)p++;printf(ThelastoneisNO.%d\n,*p);return0;}8-6#includestdio.hintmain(){intlength(char*p);intlen;charstr[20];printf(inputstring:);scanf(%s,str);len=length(str);printf(Thelengthofstringis%d.\n,len);return0;}intlength(char*p){intn;n=0;while(*p!='\0'){n++;p++;}return(n);}8-7#includestdio.h#includestring.hintmain(){voidcopystr(char*,char*,int);intm;charstr1[20],str2[20];printf(inputstring:);gets(str1);printf(whichcharacterthatbegintocopy?);scanf(%d,&m);if(strlen(str1)m)printf(inputerror!);else{copystr(str1,str2,m);printf(result:%s\n,str2);}return0;}voidcopystr(char*p1,char*p2,intm){intn;n=0;while(nm-1){n++;p1++;}while(*p1!='\0'){*p2=*p1;p1++;p2++;}*p2='\0';}8-8#includestdio.hintmain(){intupper=0,lower=0,digit=0,space=0,other=0,i=0;char*p,s[20];printf(inputstring:);while((s[i]=getchar())!='\n')i++;p=&s[0];while(*p!='\n'){if(('A'=*p)&&(*p='Z'))++upper;elseif(('a'=*p)&&(*p='z'))++lower;elseif(*p=='')++space;elseif((*p='9')&&(*p='0'))++digit;else++other;p++;}printf(uppercase:%dlowercase:%d,upper,lower);printf(space:%ddigit:%dother:%d\n,space,digit,other);return0;}8-9#includestdio.hintmain(){voidmove(int*pointer);inta[3][3],*p,i;printf(inputmatrix:\n);for(i=0;i3;i++)scanf(%d%d%d,&a[i][0],&a[i][1],&a[i][2]);p=&a[0][0];move(p);printf(Now,matrix:\n);for(i=0;i3;i++)printf(%d%d%d\n,a[i][0],a[i][1],a[i][2]);return0;}voidmove(int*pointer){inti,j,t;for(i=0;i3;i++)for(j=i;j3;j++){t=*(pointer+3*i+j);*(pointer+3*i+j)=*(pointer+3*j+i);*(pointer+3*j+i)=t;}}8-10-1#includestdio.hintmain(){voidchange(int*p);inta[5][5],*p,i,j;printf(inputmatrix:\n);for(i=0;i5;i++)for(j=0;j5;j++)scanf(%d,&a[i][j]);p=&a[0][0];change(p);printf(Now,matrix:\n);for(i=0;i5;i++){for(j=0;j5;j++)printf(%d,a[i][j]);printf(\n);}return0;}voidchange(int*p){inti,j,temp;int*pmax,*pmin;pmax=p;pmin=p;for(i=0;i5;i++)for(j=i;j5;j++){if(*pmax*(p+5*i+j))pmax=p+5*i+j;if(*pmin*(p+5*i+j))pmin=p+5*i+j;}temp=*(p+12);*(p+12)=*pmax;*pmax=temp;temp=*p;*p=*pmin;*pmin=temp;pmin=p+1;for(i=0;i5;i++)for(j=0;j5;j++)if(((p+5*i+j)!=p)&&(*pmin*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for(i=0;i5;i++)for(j=0;j5;j++)if(((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for(i=0;i5;i++)for(j=0;j5;j++)if(((p+5*i+j)!=p)&&((p+5*i+j)!=(p+4))&&((p+5*i+j)!=(p+20))&&(*pmin*(p+5*i+j)))pmin=p+5*i+j;temp=*