您的位置: 网站首页 > 程序开发 > C语言程序设计案例教程 > 第八章 指针 > 【8.4 思考与练习】

8.4 思考与练习

 

思考与练习

1.选择题

(1)设有以下语句,则      不是对a数组元素的正确引用,其中0≤i<10。

int a[10]={0,1,2,3,4,5,6,7,8,9}, *p=a;

A.a[p-a]                    B.*(&a[i])                 C.p[i]                 D.*(*(a+i))

(2)将以下程序补充完整,并实现调用findmax( )函数,求数组中值最大的元素在数组中的下标。

#include <stdio.h>

void findmax (int *s,int t,int *k)

{ 

    int p; 

    for(p=0,*k=p;p<t;p++)

        if(s[p] >s[*k])      ;

}

main()

{  

    int a[10],i,k ;

    for(i=0;i<10;i++)

       scanf("%d",&a[i]);

    findmax(a,10,&k);

    printf("%d,%d\n",k,a[k]); 

}

A.k=p                        B.*k=p-s                   C.k=p-s              D.*k=p

(3)以下程序的输出结果为      。

main()

{

    char *alpha[6]={"ABCD","EFGH","IJKL","MNOP","QRST","UVWX"};

    char **p; int i;

    p=alpha;

    for(i=0;i<4;i++)

        printf("%s",p[i]);

    printf("\n"); 

}

A.ABCDEFGHIJKL                                      B.ABCD

C.ABCDEFGHIJKLMNOP                                   D.AEIM

(4)设有以下语句:

char str[4][12]={"aaa","bbbb","ccccc","dddddd"},*strp[4];

int i; 

for(i=0;i<4;i++)

   strp[i]=str[i];

则      不是对字符串的正确引用,其中0≤k<4。

A.strp                        B.str[k]                      C.strp[k]             D.*strp

(5)设有以下语句:

char str1[]="string",str2[8],*str3,*str4="string";

则      不是对库函数strcpy( )的正确调用(此库函数用于复制字符串)。

A.strcpy(str1,"HELLO1");                              B.strcpy(str2,"HELLO2");

C.strcpy(str3,"HELLO3");                              D.strcpy(str4,"HELLO4");

(6)若有以下说明语句,      是对c数组元素的正确引用。

int c[4][5],(*cp)[5];

cp=c;

A.cp+1                      B.*(cp+3)                  C.*(cp+1)+3              D.*(*cp+2)

(7)执行以下程序片段后,ab的值为      。

int *var,ab;

ab=100

var=&ab;

ab=*var+10;

A.120                        B.110                        C.100                 D.90

(8)执行以下程序片段后,*(ptr+5)的值为      。

char str[ ]="Hello";

char *ptr;

ptr=str;

A.'o'                          B.'\o'                          C.不确定的值     D.'o'的地址

(9)以下程序的输出结果是      。

#include <stdio.h>

main()

{ 

   int **k,*j,i=100;

   j=&i;   k=&j;

   printf("%d\n",**k);

}

A.运行错误                                                  B.100                       

C.i的地址                                                    D.j的地址

(10)以下函数的功能是      。

sss(char *s,char *t)

{

    while((*s)&&(*t)&&(*t++==*s++));

    return(*s-*t);

}

A.求字符串的长度                                      B.比较两个字符串的大小

C.将字符串s复制到字符串t中                   D.将字符串s连接到字符串t后

(11)以下程序的输出结果是      。

#include<stdio.h>

int f(char *s)

{

    char *p=s;

    while(*p!='\0')

        p++;

    return (p-s);

}

main()

{

    printf("%d\n",f("ABCDEF"));

}

A.3                          B.6                            C.8                     D.0

(12)以下程序的输出结果是      。

#include <stdio.h>

#include <string.h>

main()

{

    char *s1="AbCdEf",*s2="aB";

    s1++;

    s2++;

    printf("%d\n",strcmp(s1,s2));

}

A.正数                    B.负数                      C.0                     D.不确定的值

(13)若执行以下程序时,从键盘上输入OPEN THE DOOR<CR>,则输出结果是    。

#include <stdio.h>

char fun(char *c)

{

    if(*c<='Z' && *c>='A') *c-='A'-'a';

    return *c;

}

main()

{

    char s[81],*p=s;

    gets(s);

    while(*p)

    {

        *p=fun(p);

        putchar(*p);

        p++;

    }

    putchar('\n');

}

A.oPEN tHE door                                        B.open the door   

C.OPEN THE DOOR                                          D.Open The Door

(14)若有以下程序片段:

int a[12]={0},*p[3], **pp, i;

for(i=0;i<3;i++)

   p[i]=&a[i*4];

pp=p;

则对数组元素的错误引用是      。

A.pp[0][1]               B.a[10]                      C.p[3][1]            D.*(*(p+2)+2)

(15)以下程序的输出结果是      。

#include <stdio.h>

void fun(float *p1,float *p2,float *s)

{

    s=(float*)calloc(1,sizeof(float));

    *s=*p1+*(p2++);

}

main()

{

    float a[2]={1.1,2.2},b[2]={10.0,20.0},*s=a;

    fun(a,b,s);

    printf("%f\n",*s);

}

上面程序的输出结果是      。

A.11.100000            B.12.100000                     C.21.100000              D.1.100000

(16)以下程序的输出结果是      。

main()

{

    int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a;

    printf("%d\n",*(p+2));

}

A.3                          B.4                            C.1                     D.2

(17)以下函数的功能是      。

int funl(char *x)

{

    char *y=x;

    while(*y++);

    return(y-x-1);

}

A.求字符串的长度                                      B.比较两个字符串的大小

C.将字符串x复制到字符串y中                  D.将字符串x连接到字符串y后

(18)以下程序的输出结果是      。

void prtv(int *x)

{

    printf("%d\n",++*x);

}

main()

{

    int a=25;

    prtv(&a);

}

A.23                        B.24                          C.25                   D.26

(19)以下程序有两个printf语句,如果第一个printf语句的输出结果是194,则第二个printf语句的输出结果是      。

#include <stdio.h>

main()

{

    static int a[10]={1,2,3,4,5,6,7,8,9,0},*p;

    p=a;

    printf("%x\n",p);

    printf("%x\n",p+9);

}

A.203                      B.204                        C.1a4                 D.1a6

(20)设有以下函数定义:

int f(char *s)

{

    char *p=s;

    while(*p!='\0') p++;

    return (p-s);

}

如果在主程序中用语句printf("%d\n",f("goodbey!"));调用了上述的函数,则输出结果为      。

A.3                          B.6                            C.8                     D.0

(21)设有以下定义语句:

int a[4][3]={1,2,3,4,5,6,7,8,9,10,11,12};

int (*prt)[3]=a,*p=a[0];

则以下选项中,能够正确表示数组元素a[1][2]的表达式是      。

A.*((*prt+1)[2])                                                 B.*(*(p+5))

C.(*prt+1)+2                                               D.*(*(a+1)+2)

(22)以下程序的输出结果是      。

main()

{

    char*p1,*p2,str[50]="xyz";

    p1="abcd";

    p2="ABCD";

    strcpy(str+2,strcat(p1+2,p2+1));

    printf("%s",str);

}

A.xyabcAB                     B.abcABz                  C.Ababcz            D.xycdBCD

(23)以下程序的输出结果是      。

main()

{

    int a[5]={2,4,6,8,10},*p,**k;

    p=a;

    k=&p;

    printf("%d ",*(p++));

    printf("%d\n",**k);

}

A.4  4                    B.2  2                      C.2  4               D.4  6

(24)执行以下程序后,y的值是      。

main()

{

    int a[]={2,4,6,8,10};

    int y=1,x,*p;

    p=&a[1];

    for(x=0;x<3;x++)

       y+=*(p+x);

    printf("%d\n",y);

}

A.17                        B.18                          C.19                   D.20

(25)若有以下定义语句:

int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a;

则数值为6的表达式是      。

A.*p+6                    B.*(p+6)                    C.*p+=5             D.p+5

(26)若有以下定义语句:

int w[3][4]={{0,1},{2,4},{5,8}};

int(*p)[4]=w;

则数值为4的表达式是      。

A.*w[1]+1               B.p++,*(p+1)             C.w[2][2]           D.p[1][1]

(27)以下程序的输出结果是      。

main()

{

    char ss[10]="12345";

    strcat(ss,"6789");

    *(ss+0)='A';

    *(ss+1)='B';

    *(ss+2)='C';

    printf("%s\n",ss);

}

A.ABC                                                       B.ABC9             

C.123456ABC                                             D.ABC456789

(28)若有定义语句double *p,a;,则能通过scanf( )语句正确给输入项读入数据的程序片段是      。

A.*p=&a; scanf("%lf",p);

B.p=(double *)malloc(8);scanf("%f",p);

C.p=&a;scanf("%lf",a);

D.p=&a; scanf("%le",p);

(29)若有以下程序片段:

main()

{

    int t[3][2],*pt[3],k;

    for(k=0;k<3;k++) pt[k]=t[k];

}

则以下选项中,能正确表示t数组元素地址的表达式是      。

A.&t[3][2]               B.*pt[0]                     C.*(pt+1)            D.&pt[2]

(30)若以下程序的功能是输出数组中的最大值,由s指针指向该元素。

main()

{

    int a[10]={6,7,2,9,1,10,5,8,4,3,},*p,*s;

    for(p=a,s=a;p-a<10;p++)

        if(         )  s=p;

    printf("The max is:%d",*s):

}

则在if语句中的判断表达式应该是      。

A.p>s                      B.*p>*s                     C.a[p]>a[s]         D.p-a>p-s

(31)若要求函数的功能是交换x和y中的值,且通过正确调用返回交换结果。则以下选项中,能正确表示此功能的函数是      。

A.funa(int *x,int *y)                    B.funb(int x,int y)

          {                                       {

             int *p;                                 int t;

             *p=*x; *x=*y; *y=*p;                    t=x; x=y; y=t;

          }                                      }

C.func(int *x,int *y)                 D.fund(int x,int y)

          {                                       {

           *x=*y; *y=*x;                         *x=*x+*y; *y=*x-*y; *x=*x-*y;

         }                                    }

(32)以下程序片段的输出结果是      。

int **pp,*p,a=10,b=20;

pp=&p; p=&a; p=&b; printf("%d,%d\n",*p,**pp);

A.10,20                   B.10,10                            C.20,10                     D.20,20

(33)若有以下定义语句:

char s[20]="programming",*ps=s;

则不能代表字符o的表达式是      。

A.ps+2                     B.s[2]                        C.ps[2]               D.ps+=2,*ps

(34)以下程序片段的输出结果是      。

char *s1="12345",*s2="1234";

printf("%d\n",strlen(strcpy(s1,s2)));

A.4                          B.5                            C.9                     D.10

(35)若有以下定义语句:

int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a;

则不能表示a数组元素的表达式是      。

A.*p                        B.a[10]                      C.*a                   D.a[p-a]

(36)若有以下的定义语句:

int a[]={1,2,3,4,5,6,7,8,9,10},*p=a;

则值为3的表达式是      。

A.p+=2, *(p++)        B.p+=2,*++p             C.p+=3, *p++     D.p+=2,++*p

(37)若有以下定义语句:

int w[2][3],(*pw)[3];

pw=w;

则对w数组元素非法引用的是      。

A.*(w[0]+2)             B.*(pw+1)[2]             C.pw[0][0]          D.*(pw[1]+2)

(38)设P1和P2是指向同一个int型一维数组的指针变量,k为int型变量,则不能正确执行的语句是      。

A.k=*P1+*P2;                                             B.p2=k;     

C.P1=P2;                                                    D.K=*P1*(*P2);

(39)设有如下定义:

int arr[]={6,7,8,9,10};

int *ptr;

则以下程序片段的输出结果为      。

ptr=arr;

*(ptr+2)+=2;

printf("%d,%d\n",*ptr,*(ptr+2));

A.8,10                            B.6,8                         C.7,9                  D.6,10

(40)以下程序的输出结果是      。

main()

{

    int i,k,a[10],p[3];

    k=5;

    for(i=0;i<10;i++)  a[i]=i;

    for(i=0;i<3;i++)  p[i]=a[i*(i+1)];

    for(i=0;i<3;i++)  k+=p[i]*2;

    printf("%d\n",k);

}

A.20                        B.21                          C.22                   D.23

(41)执行以下程序片段后,m的值为      。

int a[2][3]={{1,2,3},{4,5,6}};

int m,*p;

p=&a[0][0];

m=(*p)*(*(p+2))*(*(p+4));

A.15                        B.14                          C.13                   D.12

(42)设有如下定义语句:

int (*ptr)*();

则以下叙述中,正确的是      。

A.ptr是指向一维组数的指针变量

B.ptr是指向int型数据的指针变量

C.ptr是指向函数的指针变量,该函数返回一个int型数据

D.ptr是一个函数名,该函数的返回值是指向int型数据的指针变量

(43)以下程序的输出结果是      。

void fun(int x,int y,int *cp,int *dp)

{

    *cp=x+y;

    *dp=x-y;

}

main()

{

    int a,b,c,d;

    a=30;  b=50;

    fun(a,b,&c,&d);

    printf("%d,%d\n",c,d);

}

A.50,30                   B.30,50                            C.80,-20            D.80,20

(44)执行以下程序时,如果从键盘上输入ABCDE<CR>,则输出结果为      。

#include <stdio.h>

#include <string.h>

func(char str[])

{

    int num=0;

    while(*(str+num)!='\0')

        num++;

    return (num);

}

main()

{

    char str[10],*p=str;

    gets(p);

    printf("%d\n",func(p));

}

A.8                          B.7                            C.6                     D.5

(45)以下程序的输出结果是      。

#include <stdio.h>

int ss(char  *s)

{

    char *p=s;

    while(*p)  p++;

    return (p-s);

}

main()

{

    char *a="abded";

    int i;

    i=ss(a);

    printf("%d\n",i);

}

A.8                          B.7                            C.6                     D.5

(46)执行以下程序后,a的值是      。

main()

{

    int a,k=4,m=6,*p1=&k,*p2=&m;

    a=p1=&m;

    printf("%d\n",a);

}

A.4                                                             B.1

C.0                                                             D.运行时出错,a无定值

(47)以下程序的输出结果是      。

main()

{

    int i,x[3][3]={9,8,7,6,5,4,3,2,1},*p=&x[1][1];

    for(i=0;i<4;i+=2)

        printf("%d ",p[i]);

}

A.5  2                    B.5  1                      C.5  3               D.9  7

(48)以下程序的输出结果是      。

#include <stdio.h>

main()

{

    int a[]={1,2,3,4,5,6,7,8,9,10,11,12};

    int *p=a+5,*q=NULL;

    *q=*(p+5);

    printf("%d %d\n",*p,*q);

}

A.运行后报错                                             B.6  6       

C.6  12                                                      D.5  5

(49)若有以下的程序片段,则在执行for语句后,*(*(pt+l)+2)表示的数组元素是    。

int t[3][3],*pt([3],k;

for(k=0;k<3;k++)

    pt[k]=&t[k][0];

A.t[2][0]                  B.t[2][2]                    C.t[l][2]              D.t[2][l]

(50)以下程序的输出结果是      。

#include <string.h>

main()

{

    char *p1,*p2,str[50]="ABCDEFG";

    p1="abcd"; p2="efgh";

    strcpy(str+1,p2+1);

    strcpy(str+3,p1+3);

    printf("%s",str);

}

A.AfghdEFG            B.Abfhd                    C.Afghd             D.Afgd

(51)以下程序的输出结果是      。

void fun(char *c,int d)

{

    *c=*c+1; d=d+1;

    printf("%c,%c,",*c,d);

}

main()

{

    char a='A',b='a';

    fun(&b,a);

    printf("%c,%c\n",a,b);

}

A.B,a,B,a                 B.a,B,a,B                   C.A,b,A,b           D.b,B,A,b

2.填空题

(1)执行以下程序时,输入字符串how do you do,则输出结果是      。

main()

{

    char str1[]="how do you do",str2[10];

    char *p1=str1,*p2=str2;

    scanf("%s",p2);

    printf("%s",p2);

    printf("%s\n",p1);

}

 

(2)fun1( )函数的调用语句为fun1(&a,&b,&c);,它将三个整数按由大到小的顺序调整后依次放入a、b、c三个变量中,a中放最大数。

void fun2 (int *x,int *y)

{

    int t;

    t=*x; *x=*y; *y=t;

}

void fun1 (int *pa,int *pb,int *pc)

{

    if(*pc>*pb) fun2(      );

    if(*pa<*pc) fun2(      );

    if(*pa<*pb) fun2(      );

}

(3)设有以下定义语句:

int a[3][2]={10,20,30,40,50,60},(*p)[2];

p=a;

则*(*(p+2)+1)的值为      。

(4)以下函数的功能是将两个整数指针所指的存储单元中的内容进行交换。

exchange(int *x, int *y)

{

    int t;

    t=*y; *y=      ; *x=      ;

}

(5)以下程序的输出结果是      。

#include <stdio.h>

main()

{

    char ch[2][5]={"6934","8254"},*p[2];

    int i,j,s=0;

    for(i=0;i<2;i++)

        p[i]=ch[i];

    for(i=0;i<2;i++)

        for(j=0;p[i][j]>='0' && p[i][j]<='9';j+=2)

             s=10*s+p[i][j]-'0';

    printf("%d\n",s);

}

3.程序分析题

(1)分析以下程序,并写出程序的输出结果。

#include <stdio.h>

int a[]={2,4,6,8};

main()

{

    int i;

    int *p=a;

    for(i=0;i<4;i++)

        a[i]=*p++;

    printf("%d\n",a[2]);

}

(2)分析以下程序,并写出程序的输出结果。

#include<stdio.h>

main()

{

    static char b[]="Goodbye";

    char *chp=&b[7];

    while(--chp>=&b[0])

        putchar(*chp);

    putchar('\n');

}

(3)分析以下程序,并写出程序的输出结果。

main()

{

    int a[]={2,4,6},*prt=&a[0]3

    int x=8,y,z;

    for(y=0;y<3;y++)

        z=(*(prt+y)<x)?*(prt+y):x;

    printf("%d\n",z);

}

(4)分析以下程序,并写出程序的输出结果。

#include <stdio.h>

sub(int x,int y,int *z)

{

    *z=y-x;

}

main()

{

    int a,b,c;

    sub(10,5,&a);

    sub(7,a,&b);

    sub(a,b,&c);

    printf("%d,%d,%d\n",a,b,c);

}

(5)分析以下程序,并写出程序的输出结果。

#include<stdio.h>

void fun(int *s,int n1,int n2)

{

    int i,j,t;

    i=n1;j=n2;

    while(i<j)

    {

        t=*(s+i);

        *(s+i)=*(s+j);

        *(s+j)=t;

        i++,j--;

    }

}

main()

{

    int a[10]={1,2,3,4,5,6,7,8,9,0},i,*p=a;

    fun(p,0,3);

    fun(p,4,9);

    fun(p,0,9);

    for(i=0;i<10;i++)

        printf("%d",*(a+i));

    printf("\n");

}

(6)分析以下程序,并写出程序的输出结果。

#include <stdio.h>

void as(int x,int y,int *cp,int *dp)

{

    *cp=x+y;

    *dp=x-y;

}

main()

{

    int a=4,b=3,c,d;

    as(a,b,&c,&d);

    printf("%d %d\n",c,d);

}

(7)分析以下程序,并写出程序的输出结果。

#include <stdio.h>

main()

{

    int a[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};

    int (*p)[4]=a,i,j,k=0;

    for(i=0;i<3;i++)

        for(j=0;j<2;j++)

            k=k+*(*(p+i)+j);

    printf("%d\n",k);

}

(8)分析以下程序,并写出程序的输出结果。

#include <stdio.h>

void fun(int n,int *s)

{

    int f1,f2;

    if(n==1||n==2) *s=1;

    else

    {

        fun(n-1,&f1);

        fun(n-2,&f2);

        *s=f1+f2;

    }

}

main()

{

    int x;

    fun(6,&x);

    printf("%d\n",x);

}

(9)分析以下程序,并写出程序的输出结果。

#include<string.h>

void fun(char *s)

{

    int x=0,y;

    char c;

    for(y=strlen(s)-1;x<y;x++,y--)

    {

        c=s[x];

        s[x]=s[y];

        s[y]=c;

    }

}

main()

{

    char *a="abcdefgh";

    fun(a);

    puts(a);

}

(10)分析以下程序,并写出程序的输出结果。

main()

{

    char a[]="programming",b[]="language";

    char *p1,*p2;

    int i;

    p1=a;

    p2=b;

    for(i=0;i<7;i++)

        if(*(p1+i)==*(p2+i))

            printf("%c",*(p1+i));

}

4.上机操作题

(1)编写一个函数,要求从键盘上输入10个整数,按由大到小的顺序输出。要求使用函数和指针。

(2)编写一个函数,用来求一个字符串的长度,并在主函数中编写代码测试该函数。

(3)编写一个函数,将一个3′3的整型矩阵转置。要求使用函数和指针,并在主函数中编写代码测试该函数。

(4)编写一个函数,将3′4的整型矩阵的右上半角的元素置为0,并在主函数中编写代码测试该函数。

(5)编写一个函数,实现两个字符串的比较,并在主函数中编写代码测试该函数。

(6)编写一个函数,要求从键盘上输入10个学生的成绩,求其中的最高分、最低分、平均分。