第9章指针概述.ppt

第9章 指 针 本章要点: 指针与指针变量的定义 指针与数组的关系 指针与字符串的关系 指针与函数的关系 指针的应用 上机实验 1.掌握指针、指针变量的定义; 2.掌握指针的应用方法和注意事项; 3.掌握指针与数组、指针与字符串和指针与函数的关系。 一、实验目的 1.验证性实验 1)阅读下面的程序,并上机运行该程序。 #include stdio.h sss(char *s) { char *p=s; while(*p) p++; return(p-s); } main( ) { char *a=975321; int n; n=sss(a); printf(%d,n); } 二、实验内容 2)阅读下面的程序,并上机运行该程序。 #include stdio.h main( ) { int a[]={0,1,2,3,4,5,6,7,8,9}; int s,i,*p; s=0; p=a[0]; for(i=0;i10;i+=2) s+=*(p+i); printf(s=%d,s); } 3)阅读下面的程序,并上机运行该程序。 #include stdio.h main ( ) { char *a=I wish you success! ; int k; printf (%c\n,a[7]); for ( k=0;a[k]!=\0;k++) printf(%c,a[k]); } 4)下面给定程序中fun函数的功能是:分别统计字符串中大写字母和小写字母的个数。请改正程序中的错误,使它能计算出正确的结果。 #include stdio.h void fun(char *s,int a,int b) { while(*s) { if(*s=A*s=Z) a++; if(*s=a*s=z) b++; s++; }} main( ) { char s[100]; int upper=0,lower=0; printf(\nPlease a string: ); gets(s); fun(s,upper,lower); printf(\n upper=%d lower=%d\n,upper,lower); } 2.设计性实验 1)函数的功能是:将a、b中的两个两位正整数合并形成一个新的整数放在c中。合并的方式是:将a中的十位和个位数依次放在变量c的千位和十位上,b中的十位和个位数依次放在变量c 的个位和百位上。 例如,当a=45, b=12,调用该函数后 c=4251。 #include stdio.h void fun(int a, int b, long *c) { } main() { int a,b; long c; printf(Input a, b:); scanf(%d%d, a, b); fun(a, b, c); printf(The result is: %d\n, c); }} 2)将函数fun补充完整。函数fun的功能是:将s所指字符串中ASCII值为偶数的字符删除,串中剩余字符形成一个新串放在t所指的数组中。 #include stdio.h #include string.h void fun(char *s1, char t1[]) { } main() { char s[100], t[100]; printf(\nPlease enter string S:); scanf(%s, s); fun(s, t); printf(\nThe result is: %s\n, t); } 3)请编写函数fun,求出ss所指字符串指定字符的个数,并返回此值。 #include stdio.h #include string.h #define M 81 int fun(char *ss, char c) { } main() { char a[M], ch; printf(\nPlease enter a string:); gets(a); printf(\nPlease enter a char:); ch = getchar(); printf(\nThe number of the char is: %d\n, fun(a, ch)); } * 学习方法建议:   本章内容是C语言中最难的部分,学习时应注意

文档评论(0)

1亿VIP精品文档

相关文档