厦门大学本科生公共课 《C程序设计基础》 教材习题答案 第10章 指针
一、 选择题
1. 设已定义“int a, * p”,下列赋值表达式中正确的是 :C)p=&a 2. 设已定义“int x,*p=&x;”,则下列表达式中错误的是:B)&*x
3. 若已定义“int a=1 ,*b=&a;”,则“printf(“%d \\n”,*b);”的输出结果为:A) a的值。
4. 设已定义“int x,*p,*pl=&x,*p2=&x;”,则下列表达式中错误的是:C)p=p1+p2. 5. 设有函数定义“void p(int *x){printf(“%d\\n”,*x);}”和变量定义“int a=3;”,则正确的函数
调用是:C)p(&a)
6. 函数“int fun(char * x){char * y=x; while(*y)y++;return(y-x); }”的功能是A)求字
符串的长度。
7. 运行一下程序,输出结果为:B)5 6
int fun (int a,int *b) {
a++;(*b)++; return a+*b; }
void main() {int x=1,y=2;
Printf(“%d”,fun(x,&y)); Printf(“%d”,fun(x,&y)); }
8. 运行以下程序,输出结果为:C)58
#include
Void main() {
Int x=1,y=2,*z; Z=fun(x,&y); Printf(“%d”,*z); Z=fun(x,&y); Printf(“%d”,*z); }
9. 若已定义“int a[]={1,2 ,3,4},*p=a;”,则下面表达式中值不等于2的是C)*(++a) 10. 若已定义“int a[]={1,2 ,3,4},*p=a+1;”,则p[2]的值为C)4
11. 设已定义“int x[4][10],*p=x[0];”,则下列表达式中的值为整形的是B)*(p+1) 12. 设已定义“char s[]=”ABCD”;”,”printf(“%s”,s+1)”的值为C)BCD
第1页/共7页
厦门大学本科生公共课 《C程序设计基础》 教材习题答案 第10章 指针
13. 设已定义“char str[]=”abcd”,*ptr=str;”,则*(prt+4)的值为B)0
14. 下面对字符串变量的初始化或赋值操作中,错误的是C)char a[10];a=”OK”; 15. 设已定义“char *ps[2]={“abc”,”1234”};”,则以下叙述中错误的是A)ps为指针变
量,它指向一个长度为2的字符串数组
16. 设已定义“struct {int a,b;} s,*ps=&s;”,则错误的结构体成员引用是C)*ps.a 17. 设已有以下定义,则表达式的值为2的是A)k=++p->data
struct st { int data; st *link;
} a[3]={1,a+1,3,a+2,5,0},*p=a; 二、 编程题
1. 输入3个字符串,输出其中最大的字符串(用字符指针)
#include
int main(int argc, char *argv[]) {
char a[str_count][str_length],*p; printf(\请输入3个字符串:\ int i;
for(i=0;i p=a[0]; for(i=1;i printf(\最大的字符串为:%s\ system(\ return 0; } 2. 定义一个函数,函数的功能是求已知半径的圆的周长和面积。要求把半径、周长和 面积设置成函数参数。 #include 第2页/共7页 厦门大学本科生公共课 《C程序设计基础》 教材习题答案 第10章 指针 int main(int argc, char *argv[]) { double r; printf(\请输入半径:\ scanf(\ double c,s; void circle(double r, double *,double *); circle(r,&c,&s); printf(\圆的周长和半径分别为:%lf,%lf\ system(\ return 0; } void circle(double r,double *c,double *s) { *c=2*pi*r; *s=pi*r*r; } 3. 定义函数max,函数参数为3个字符串,函数返回值最大的字符串。 #include int main(int argc, char *argv[]) { char a[str_count][str_length]; printf(\请输入三个字符串:\\n\ int i; for(i=0;i char * max(char * a1,char *a2,char *a3); printf(\最大的字符串是:%s\ system(\ return 0; } char * max(char * a1,char *a2,char *a3) { char * p; p=a1; if(strcmp(p,a2)<0) { 第3页/共7页 厦门大学本科生公共课 《C程序设计基础》 教材习题答案 第10章 指针 p=a2; } if(strcmp(p,a3)<0) { p=a3; } return p; } 4. 自己定义字符串复制函数,然后调用之 #include int main(int argc, char *argv[]) { char *s=\ char *o; char * str_copy_self(char *s); o=str_copy_self(s); printf(\ system(\ return 0; } char * str_copy_self(char *s) { int str_length=strlen(s); char *o=(char *)malloc((str_length+1)*sizeof(char)); int i; for(i=0;i 5. 定义一个函数,函数参数为一维数组(用指针表示),函数返回数组元素的平均 值。 #include int main(int argc, char *argv[]) { double arr[]={1,2,3,4,5,6,7,8,9,10}; double ave(double arr[],int arr_len); 第4页/共7页 厦门大学本科生公共课 《C程序设计基础》 教材习题答案 第10章 指针 printf(\ system(\ return 0; } double ave(double arr[],int arr_len) { int i; double res=0; for(i=0;i 6. 定义一个函数,删除字符串中第k个字符开始的m个字符,例如删除字符串 abcde第2个字符开始的3个字符,则删除后结果为ae;又如删除字符串abcde第4个字符开始的5个字符,则删除后结果为abc; #include int main(int argc, char *argv[]) { char str[]=\ void str_pruning(char * str,int begin_pos,int pruning_count); str_pruning(str,4,5); printf(\ system(\ return 0; } void str_pruning(char * str,int begin_pos,int pruning_count) { int str_len=strlen(str); if(begin_pos>=str_len) { str[0]='\\0'; } else if((begin_pos-1+pruning_count)>=str_len) { str[begin_pos-1]='\\0'; } 第5页/共7页 厦门大学本科生公共课 《C程序设计基础》 教材习题答案 第10章 指针 else { int i=0; while(1) { str[begin_pos+i-1]=str[begin_pos+pruning_count+i-1]; if(str[begin_pos+pruning_count+i-1]=='\\0') { break; } i++; } } } 7. 在字符串中删除所有指定字符(如把字符串teacher中的e字符删除,得到 tachr),使用子函数和字符指针。 #include int main(int argc, char *argv[]) { char str[]=\ void char_punching(char * str, char l); char_punching(str,'e'); printf(\ system(\ return 0; } void char_punching(char * str, char l) { int c=0; int str_len=strlen(str); int i; for(i=0;i 第6页/共7页 厦门大学本科生公共课 《C程序设计基础》 教材习题答案 第10章 指针 { str[i-c]=str[i]; } } str[str_len-c]='\\0'; } 8. 求二维数组的最大元素值及最大元素的位置(用指针法引用数组元素) #include int main(int argc, char *argv[]) { int a[3][3]={{1,2,333},{4,15,6},{7,8,9}}; int x=3,y=3,i,j; int px=0,py=0; int *p,*p_max; p_max=a[0]; for(i=0;i printf(\ system(\ return 0; } 第7页/共7页