收藏 分销(赏)

实验七指针及其运算.doc

上传人:仙人****88 文档编号:9280549 上传时间:2025-03-19 格式:DOC 页数:6 大小:47.04KB 下载积分:10 金币
下载 相关 举报
实验七指针及其运算.doc_第1页
第1页 / 共6页
实验七指针及其运算.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
实验七 指针及其运算 实验目的 理解指针的概念,会定义和使用指针变量,掌握指针的运算规则,学会使用数组的指针和指向数组的指针变量。 要点提示 1. 变量在内存单元的地址称为该变量的“指针”。 2. 指针变量是用来存放其它变量地址的变量。 3. 指针变量定义的一般形式: 类型标识符 *指针变量名; 4. 指针运算符 &(取变量地址运算符)和*(取值运算符)是互逆的运算符,右结合。 ++,--,+,- 运算符是按地址计算规则进行的。 5. 用指针作为函数参数,可以实现“调用函数改变变量的值,在主调函数中使用这些改变的值。 6. 数组的指针是指数组的起始地址(首地址),数组元素的指针是指数组元素的地址。 数组名代表数组的首地址,是地址常量。 实验内容 1. 指针变量的说明,初始化和使用。 2. 指针作为函数参数。 3. 数组的指针和指针数组的指针变量的说明和使用 4. 数组名作为函数参数 实验步骤 实验1 main() { int *p1,*p2, 【 】; /* 指针变量说明语句 */ int a, b; printf("\n Please input a and b:"); scanf("%d%d",&a,&b); p1=&a; p2=&b; if (a<b) { p=p1; p1=p2; p2=p;} /* 将p1和p2变量的值进行交换 */ printf("max=%d,min=%d\n",【 】); } 程序运行结果: Please input a and b: 12 34 【 】 main() { int *p1,*p2,*p; /* 指针变量说明语句 */ int a,b; printf("\n Please input a and b:"); scanf("%d%d",&a,&b); p1=&a; p2=&b; if (a<b) { p=p1; p1=p2; p2=p; } /* 将p1和p2变量的值进行交换 */ printf("max=%d,min=%d\n",*p1,*p2); } Please input a and b:12 34 max=34,min=12 Press any key to continue 实验2 main() { int a,b,c; int *p1,*p2,*p3; printf("\nInput 3 numbers:"); scanf("%d%d%d", &a,&b,&c); p1=&a; p2=&b; p3=&c; if (a>b) swap(【 】); /* 函数调用 */ if (a>c) swap(【 】); /* 函数调用 */ if (b>c) swap(【 】); /* 函数调用 */ printf("\nThe sorted numbers: %d,%d,%d\n",a,b,c); } swap(int *p1, int *p2) { 【 】; p=*p1; *p1=*p2; *p2=p; } 程序运行结果: Input 3 numbers: 100 200 150 【The sorted numbers: 100 150 200 】 main() { int a,b,c; int *p1,*p2,*p3; printf("\nInput 3 numbers:"); scanf("%d%d%d", &a,&b,&c); p1=&a; p2=&b; p3=&c; if (a>b) swap(p1,p2); /* 函数调用 */ if (a>c) swap(p1,p3); /* 函数调用 */ if (b>c) swap(p2,p3); /* 函数调用 */ printf("\nThe sorted numbers: %d,%d,%d\n",a,b,c); } swap(int *p1, int *p2) { int p; p=*p1; *p1=*p2; *p2=p; } 实验3 #define N 5 main() { int *p,i,a[N]; p=a; printf("\nInput 5 numbers:"); for (; p<a+N; p++) scanf("%d", 【 】); 【 】; printf("\nOutput array:"); for (; p<a+N; p++) printf("%d ",【 】); } 程序运行结果: Input 5 numbers: 1 3 5 7 9 Output array:【 】 #define N 5 main() { int *p,i,a[N]; p=a; printf("\nInput 5 numbers:"); for (; p<a+N; p++) scanf("%d",p); p=a; printf("\nOutput array:"); for (; p<a+N; p++) printf("%d ",*p); } Input 5 numbers:1 3 5 7 9 Output array:1 3 5 7 9 Press any key to continue 实验4 main() {int a[6],x,i; printf("\nInput 5 numbers:"); for (i=1; i<=5; i++) scanf("%d",a+i); /* 读数到a[1]…a[5] */ printf("\nInput x:"); scanf("%d", &x); *a=x; i=5; /* 将x存入a[0]中 */ while (x!=*(a+i)) 【 】; if (【 】) printf("%5d's position is: %4d\n",x,i); else printf("%5d Not been found!\n",x); } 程序运行结果: Input 5 numbers: 1 3 5 7 9 Input x: 3 【 】 main() { int a[6],x,i; printf("\nInput 5 numbers:"); for (i=1; i<=5; i++) scanf("%d",a+i); /* 读数到a[1]…a[5] */ printf("\nInput x:"); scanf("%d", &x); *a=x; i=5; /* 将x存入a[0]中 */ while (x!=*(a+i)) i--; if (i!=0) printf("%5d's position is: %4d\n",x,i); else printf("%5d Not been found!\n",x); } Input 5 numbers:1 3 5 7 9 Input x:3 3's position is: 2 Press any key to continue 实验5 main() { float average(); static int score[5]={60,70,80,90,80}, i; float aver; printf("\Scores:"); for (i=0; i<5; i++) printf("%d ", *(score+i)); aver= 【 】 printf("\nAverage score: %6.2f",aver); } float average(int *array) {int i; float aver,sum=0; for (i=0;i<5;i++) sum=sum + *(array+i); aver=sum/5; 【 】 } 程序运行结果: 【Scores: 60 70 80 90 80】 【Average score: 76.00 】 main() { float average(); static int score[5]={60,70,80,90,80}, i; float aver; printf("\Scores:"); for (i=0; i<5; i++) printf("%d ", *(score+i)); aver=average(score); printf("\nAverage score: %6.2f",aver); } float average(int *array) { int i; float aver,sum=0; for (i=0;i<5;i++) sum=sum + *(array+i); aver=sum/5; return aver; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服