第九章 指针

#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   用返回指针的函数,要求输入每个学生编号后,输出该学生四门课的成绩及其平均成绩

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第五版的C语言程序设计第九章主要讲解了用户自己建立数据类型,包括结构体和联合体的定义、初始化、访问和使用等内容。具体包括以下几个方面: 1. 结构体的定义和使用 结构体是一种用户自定义的数据类型,可以将不同类型的数据组合在一起,形成一个新的数据类型。结构体的定义格式为: ```c struct 结构体名 { 数据类型1 成员名1; 数据类型2 成员名2; ... }; ``` 结构体的成员可以通过“.”运算符进行访问,例如: ```c struct Student { char name[20]; int age; float score; }; struct Student stu1 = {"Tom", 18, 90.5}; printf("Name: %s, Age: %d, Score: %.1f\n", stu1.name, stu1.age, stu1.score); ``` 2. 结构体指针的使用 结构体指针可以指向结构体变量,也可以指向动态分配的结构体内存。结构体指针的定义格式为: ```c struct 结构体名 *指针变量名; ``` 结构体指针的成员访问可以使用“->”运算符,例如: ```c struct Student stu2 = {"Jerry",20, 85.0}; struct Student *p = &stu2; printf("Name: %s, Age: %d, Score: %.1f\n", p->name, p->age, p->score); ``` 3. 联合体的定义和使用 联合体是一种特殊的结构体,所有成员共用同一块内存空间,不同成员的值会互相影响。联合体的定义格式为: ```c union 联合体名 { 数据类型1 成员名1; 数据类型2 成员名2; ... }; ``` 联合体的成员访问可以使用“.”运算符,例如: ```c union Data { int i; float f; char str[20]; }; union Data data; data.i = 10; printf("data.i: %d\n", data.i); data.f = 3.14; printf("data.f: %.2f\n", data.f); strcpy(data.str, "hello"); printf("data.str: %s\n", data.str); ``` 4. 枚举类型的定义和使用 枚举类型是一种用户自定义的数据类型,可以将一组相关的常量定义为一个枚举类型。枚举类型的定义格式为: ```c enum 枚举类型名 { 枚举常量1, 枚举常量2, ... }; ``` 枚举类型的变量可以直接赋值为枚举常量,例如: ```c enum Color {RED, GREEN, BLUE}; enum Color c = GREEN; printf("c: %d\n", c); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值