C语言程序设计答案(黄保和编)第10章

2018-10-22 16:04

厦门大学本科生公共课 《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 Int * fun(int a ,int *b) {a++;(*b)++; *b=a+*b; return b; }

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 #include #define str_count 3 #define str_length 100

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 #include #define pi 3.1415926

第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 #include #define str_length 100 #define str_count 3

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 #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 #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 #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 #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页


C语言程序设计答案(黄保和编)第10章.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:珍惜爱情的经典语录

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: