#include <stdio.h>
int main(){
char **p,*address[]={"china","japan","usa",""};
p=address;
for(;**p!='\0';*p++)
printf("%s\r\n",*p);
return 0;
}
/*
china
japan
usa
*/
9-1
#include <stdio.h>
int main(){
int x,*p;
x=40;
p=&x;
printf("%d %d\r\n",x,p);
x=78;
*p=x;
printf("%d %d\r\n",x,*p);
return 0;
}
/*
40 1703740
78 78
*/
9-3
#include <stdio.h>
int main(){
int a[10],r,*p,*q;
p=&a[2];
q=&a[4];
printf("q=%p\tp=%p\tq-p=%d\r\n",q,p,q-p);
return 0;
}
/*
q=0019FF28 p=0019FF20 q-p=2
*/
9-4 计算a中10个元素之和
#include <stdio.h>
int main(){
int a[10],s=0,*p,*q;
for(p=a+9,q=a;p>=q;p--)
scanf("%d",p);
q=a+10;
p=a;
while(p<q){
s+=*p;
p++;
}
printf("sum=%d\r\n",s);
return 0;
}
/*
1 1 1 1 1 1 1 1 1 1
sum=10
*/
9-5 将字符数组s1中的字符串复制到s2中
#include <stdio.h>
int main(){
char s1[20],s2[20],*p1=s1,*p2=s2;
scanf("%s",s1);
for(;*p1!='\0';p1++,p2++)
*p2=*p1;
*p2='\0';
printf("%s\r\n",s2);
return 0;
}
/*
fuckyoucai
fuckyoucai
*/
9-6利用字符型指针变量引用字符串中字符。
#include <stdio.h>
int main(){
char *p="this is c test";
for(;*p!='\0';)
putchar(*p++);
return 0;
}
/*
this is c test
*/
给指向字符型的指针变量赋字符串初值相当于定义了一个无名字符串数组。
9-7
#include <stdio.h>
int main(){
char *p="this is c test";
int i=0;
while(p[i])
printf("%c",p[i++]);
return 0;
}
/*
this is c test
*/
9-8 求字符串“I love china‘的长度
#include <stdio.h>
int main(){
char s[20],*p=s;
printf("Please input a string\r\n");
gets(s);
while(*++p);
printf("length=%d",p-s);
return 0;
}
/*
Please input a string
i love china
length=12
*/
9-9
#include <stdio.h>
int main(){
char *p,a='A',b='B';
p="a=%d,b=%d\n";
printf(p,a,b);
return 0;
}
/*
a=65,b=66
*/
p指向格式字符串,代替printf中的格式字符串
9-10 利用指针显示一个字符串happy
#include <stdio.h>
void displaystr(char *a){
printf("%s\r\n",a);
}
int main(){
char *p;
p="Happy";
displaystr(p);
return 0;
}
/*
Happy
*/
9-11
#include <stdio.h>
void displaystr(char *a){
char s[]="Happy";
printf("%s\r\n",a);
a=s;
printf("%s\r\n",a);
}
int main(){
char *p="Sadness";
displaystr(p);
printf("%s\r\n",p);
return 0;
}
/*
Sadness
Happy
Sadness
*/
9-13 将字符串的字符按照逆序输出
#include <stdio.h>
#include <string.h>
#define M 80
void reverse(char *a){
int length,k;
char *p;
length=strlen(a);
printf("len=%d\r\n",length);
for(p=a+length-1;a<p;a++,p--){
k=*p;
*p=*a;
*a=k;
}
}
int main(){
char str[M];
printf("input strings\r\n");
gets(str);
reverse(str);
printf("revers string is %s\r\n",str);
return 0;
}
/*
input strings
this is string1
len=15
revers string is 1gnirts si siht
*/
9-14 将给定字符串的第一个字母标称大写,其余变成小写
#include <stdio.h>
#include <string.h>
#define M 80
char *str(char *s){
int i=1;
if(*s>='a'&&*s<='z')
*s=*s-32;
while(*(s+i)!='\0'){
if(*(s+i)>='A'&&*(s+i)<='Z')
*(s+i)=*(s+i)+32;
i++;
}
return s;
}
int main(){
char str1[M];
printf("input strings\r\n");
gets(str1);
str(str1);
printf("correct string is %s\r\n",str1);
return 0;
}
/*
i AM A CHINESE
correct string is I am a chinese
*/
9-15 在给定子串中寻找特定字符x,若找到,则返回在s中出现的第一次位置。并将该字符及其之前的字符按照逆序输出。
#include <stdio.h>
#include <string.h>
#define M 80
char *str(char *s,char x){
int c=0;
while(x!=s[c]&&s[c]!='\0'){
c++;
}
return (&s[c]);
}
int main(){
char str1[M],*p,x;
printf("input strings\r\n");
gets(str1);
x=getchar();
p=str(str1,x);
if(*p){
printf("%c",*p);
while(p-str1){
p--;
printf("%c",*p);
}
}
else
printf("cannot find the char\r\n");
return 0;
}
/*
input strings
this is a test string
e
et a si siht
*/
9-16
#include <stdio.h>
#include <math.h>
#define M 80
double f(double z){
double d;
d=pow(z,4.0)+z-1;
return d;
}
int main(){
int i;
double r,x,(*y)(double);
y=f;
for(i=1;i<=4;i++){
x=i+0.5;
r=(*y)(x);
printf("x=%f,y=%f\r\n",i+0.5,r);
}
return 0;
}
/*
x=1.500000,y=5.562500
x=2.500000,y=40.562500
x=3.500000,y=152.562500
x=4.500000,y=413.562500
*/
先将函数f的地址赋值给指向函数的指针变量y,然后利用y调用f
9-17
#include <stdio.h>
#include <math.h>
#define M 80
double ope(double (*p1)(double),double (*p2)(double),double q){
return (2*(*p1)(q)-(*p2)(2*q));
}
int main(){
double ope(),x;
x=3.1415926/180;
printf("x=15,y=%10.6f\n",ope(sin,cos,15*x));
printf("x=30,y=%10.6f\n",ope(sin,cos,30*x));
printf("x=45,y=%10.6f\n",ope(sin,cos,45*x));
return 0;
}
/*
x=15,y= -0.348387
x=30,y= 0.500000
x=45,y= 1.414214
*/
9-18
#include <stdio.h>
int i,j;
void matrix(int *x,int *y,int *z){
for(i=0;i<3;i++){
for(j=0;j<4;j++){
*(z+i*4+j)=*(x+i*4+j)+*(y+i*4+j);
}
}
}
int main(){
int *p,a[3][4],b[3][4],c[3][4];
printf("a:\r\n");
for(i=0;i<3;i++){
for(j=0;j<4;j++){
*(a[i]+j)=i*2+j;
printf("%d ",*(a[i]+j));
}
printf("\r\n");
}
printf("b:\r\n");
for(i=0;i<3;i++){
for(j=0;j<4;j++){
*(*(b+i)+j)=j*2+j;
printf("%d ",*(*(b+i)+j));
}
printf("\r\n");
}
matrix(*a,b[0],&c[0][0]);
printf("c:\r\n");
for(i=0,p=c[0];p<c[0]+12;p++,i++){
if(i%4==0)
printf("\n");
printf("%-4d",*p);
}
}
/*
a:
0 1 2 3
2 3 4 5
4 5 6 7
b:
0 3 6 9
0 3 6 9
0 3 6 9
c:
0 4 8 12
2 6 10 14
4 8 12 16
*/
9-19 求N阶方阵副对角线上元素之和
#include <stdio.h>
#define M 4
int main(){
int a[M][M];
int i,j,sum=0,*p[M];
for(i=0;i<M;i++){
p[i]=a[i];
}
for(i=0;i<M;i++)
for(j=0;j<M;j++)
scanf("%d",p[i]+j);
for(i=0;i<M;i++)
for(j=0;j<M;j++)
if(i+j==M-1)
sum+=p[i][j];
printf("sum=%d\r\n",sum);
return 0;
}
/*
1 2 3 4
2 3 4 5
4 5 6 7
1 2 3 4
sum=14
*/
9-20 利用数字月份查找其英文名月份
#include <stdio.h>
char *monthName(int n){
char *name[]={"Illegal month","January","February","March","April","May","June","July","August","September","October","November","December"};
return ((n>0&&n<=12)?name[n]:name[0]);
}
int main(){
int n;
scanf("%d",&n);
printf("%d month name is %s\r\n",n,monthName(n));
return 0;
}
/*
8
8 month name is August
*/
以下为二级指针实例
9-23
#include <stdio.h>
void swap(int **m,int **n){
int *i;
i=*m;//这里修改的是地址
*m=*n;
*n=i;
}
int main(){
int a,b,*pa,*pb;
pa=&a;
pb=&b;
scanf("%d%d",&a,&b);
swap(&pa,&pb);
printf("pa=%d,pb=%d\r\n",*pa,*pb);
printf("a=%d,b=%d\r\n",a,b);
return 0;
}
/*
12 25
pa=25,pb=12
a=12,b=25
*/
9-24
#include <stdio.h>
int main(){
char **p,*address[]={"china","japan","usa",""};
p=address;
for(;**p!='\0';*p++)
printf("%s\r\n",*p);
return 0;
}
/*
china
japan
usa
*/
9-25
#include <stdio.h>
int main(){
int x[5]={2,4,6,8,10},*y[5],**a,b,i;
for(i=0;i<5;i++){
y[i]=&x[i];
}
a=y;
for(b=4;b>=0;b--){
printf("%3d",**a);
a++;
}
return 0;
}
/*
2 4 6 8 10
*/
利用二级指针输出数组各元素的值。指针数组名作为函数实参时,形参得到的是实参指针数组的地址,这是一个二级指针。
9-26 void指针
#include <stdio.h>
int main(){
float a[5]={2,4,6,8,10},*p1,*p3;
void *p2=a;
p1=(float *)p2;
p2=(void *)p1;
p3=&a[2];
p2=p3;
p3=p2;
printf("a=%p,p1=%p,p2=%p,p3=%p,*p3=%f",a,p1,p2,p3,*p3);
return 0;
}
/*
a=0019FF2C,p1=0019FF2C,p2=0019FF34,p3=0019FF34,*p3=6.000000
*/
由此可见,指针void起到了通用指针的作用
9-27 malloc函数
#include <stdio.h>
#include <stdlib.h>
//求一维数组之和
int main(){
int i,n,sum=0,*p;
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++){
scanf("%d",&p[i]);
sum=sum+p[i];
}
free(p);
printf("sum=%d",sum);
return 0;
}
/*
5
12 25 36 45 25
sum=143
*/
9-28 命令行
#include <stdio.h>
#include <stdlib.h>
//求一维数组之和
int main(int argc,char *argv[]){
int i=1;
printf("argc = %d\r\n",argc);
printf("argv=\n");
while(i<argc){
printf("%s\n",argv[i]);
i++;
}
return 0;
}
/*
C:\Windows\System32\cmd.exe
F:\Windows\Debug>TEST shenyang liaoning China
argv=4
shenyang
liaoning
China
*/
编译C文件后会生成TEST.exe文件,在其目录下启动命令行,在命令行下输入
TEST shenyang liaoning China
9-29
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double change(double x,double y,double *p){
double m;
m=sqrt(x*x+y*y);
*p=atan(y/x);
return m;
}
int main(int argc,char *argv[]){
double a,b,c,q;
scanf("a=%lf b=%lf",&a,&b);
c=change(a,b,&q);
printf("c=%f,q=%f",c,q);
return 0;
}
/*
a=3.0 b=5.0
c=5.830952,q=1.030377
*/
9-31 将10个国家的名字按字母由小到大的顺序输出
#include <stdio.h>
#include <string.h>
#include <math.h>
void sort(char *name[],int n){
char *temp;
int i,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[k];
name[k]=name[i];
name[i]=temp;
}
}
}
}
int main(){
int n=10,i;
char *name[]={
"China","America","Japan","Australia","Italy","Germany","Camada","Britain","France"
,"Tayland"
};
sort(name,n);
for(i=0;i<n;i++)
printf("%s\r\n",name[i]);
return 0;
}
/*
America
Australia
Britain
Camada
China
Germany
Italy
Japan
France
Tayland
*/
9-32 实现字符串str1和str2的连接
#include <stdio.h>
#include <string.h>
#include <math.h>
void string(char *str1,char *str2){
while(*++str1!='\0');
while((*str1++=*str2++)!='\0');
}
int main(){
char ch1[40],ch2[20],*p1=ch1,*p2=ch2;
printf("get ch1\r\n");
gets(ch1);
printf("get ch2\r\n");
gets(ch2);
string(p1,p2);
printf("ch1+ch2=%s\r\n",ch1);
return 0;
}
/*
get ch1
123456789a
get ch2
10bbbb
ch1+ch2=123456789a10bbbb
9-33 用返回指针的函数,要求输入每个学生编号后,输出该学生四门课的成绩及其平均成绩