第8章-指针练习题
{
t=*(p+i);
*(p+i)=*(p+8-i);
*(p+8-i)=t*10;
}
for(i=0;i<9;++i)
printf("%d,",a[i]);
}
【17】#include "stdio.h"
sub1(char *a,char b)
{char c;c=*a;*a=b;b=c;}
sub2(char a,char b)
指针思考题
一、填空题
【1】下面函数要求用来求出两个整数之和,并通过形参传回两数相加之和值,请填空。
int add( int x, int y,)
{=x+y;}
【2】若有定义:char ch;
(1)使指针p可以指向变量ch的定义语句是。
(2)使指针p指向变量ch的赋值语句是。
(3)通过指针p给变量ch读入字符的scanf函数调用语句是。
}
void test(int x,int *y)
{
int t;
t=x;x=*y;*y=t;
}
【15】#define PRINTA printf("%d,%d\n",a,b);
#define PRINTB printf("%d,%d\n",*pa,*pb);
void swap(int *p,int *q)
{
int a=89,b=10,*c;
c=&a;
opr(a,b,c);
printf("%d,%d,%d",a,b,*c);
}
【19】main()
{
int **k,*a,b=100;
a=&b;k=&a;
printf("%d\n",**k);
}
【20】fun1(int x,int y,int *cp,int *dp)
{ printf(%d\n”,++*x); }
main( )
{ int a=25;
fun (&a);
}
【7】以下程序的输出结果是( )。
A.6B.7C. 8D. 9
main()
{
int k=2,m=4,n=6;
int *pk=&k,*pm=&m,*p;
*(p=&n)=*pk*(*pm);
printf("%d\n",n);
a='A';b='B';sub1(&a,b);putchar(a);putchar(b);
}
【18】void exc(int *i,int *j)
{
int k;
k=*i;*i=*j;*j=k;
}
void opr(int x,int y,int *z)
{
exc(&x,&y);*z=x%y;
}
main()
【4】若有定义:int x,*pb;则正确的赋值表达式是( )。
A.pb=&xB.pb=xC.*pb=&xD.*pb=*x
【5】对于类型相同的指针变量,不能进行的运算是( )。
A.<B. =C. +D.–
【6】以下程序的输出结果是( )。
A.23B.24C. 25D. 26
Void fun ( int *x)
main()
{
char a[]="123456789",*p;
int i=0;
p=a;
while(*p)
{
if(i%2==0) *p='*';
p++;i++;
}
puts(a);}【2 Nhomakorabea】main()
{
int x[]={0,1,2,3,4,5,6,7,8,9};
int s,i,*p;
s=0;
p=&x[0];
{
int t;
t=*p;*p=*q;*q=t;
}
main( )
{
int a=1,b=2,*pa=&a,*pb=&b;
PRINTA
PRINTB
swap(pa,pb);
PRINTA
PRINTB
}
【16】main()
{
int a[]={1,2,3,4,5,6,7,8,9};
int *p=a,i,t;
for(i=0;i<9/2;i++)
{
*cp=x*y;
*dp=x%y;
}
main()
{
int a,b,c,d;
a=2;b=3;
ast(a,b,&c,&d);
printf("%d,%d\n",c,d);
}
【10】以下程序的输出结果是( )。
A.1,3B. 2,3C. 1,6D.2,6
main()
{
int a[]={1,2,3,4,5,6},*p;
{
int *p;
*p=*p1; *p1=*p2;*p2=*p;
}
【12】void add (int a,int b,int c)
{ c=a+b;
return c;
}
main( )
{ int *ap,*bp,a,b,c;
ap=&a,bp=&b;
scanf(“%d %d”,&ap,&bp);
add(ap,bp,c);
main()
{
int x=9,y=5,*px=&x,*py=&y;
fun(px,py);
printf("%d,%d\n",x,y);
}
【23】sub(float x,float *y,float *z)
{
*y=*y-1.0;
*z=*z+x;
}
main()
{
float a=2.5,b=9.0,*pa,*pb;
p=a;
*(p+3)+=2;
printf("%d,%d\n",*p,*(p+3));
}
三、改错题
【11】main( )
{int a=1,b=2;
printf(“a=%db=%d\n”,a,b);
swap(a,b);
printf(“a=%d b=%d\n”,*a,*b);
}
swap ( int *p1, int *p2)
{
*cp=x+y;*dp=x-y;
}
fun2(int *a,int b)
{
int c;
c=*a;*a=b;b=c;
}
main()
{
int a,b,c,d;
a=30;b=50;
fun1(a,b,&c,&d);
fun2(&c,d);
printf("%d,%d\n",c,d);
}
【21】void sub(int x,int y,int *z)
printf("%c",*(p1+k));
}
【25】main()
{
int i,k;
for(i=0;i<4;i++)
{
k=sub(&i);
printf("%3d",k);
}
printf("\n");
}
sub(int *s)
{
static int t=0;
t=*s+t;
}
【26】#include "stdio.h"
(4) 通过指针p给变量ch赋字符A的语句是。
(5)通过指针p输出ch中字符的语句是。
(6)在16位微机上,指针变量p在内存中所占字节数是,变量ch在内在所占字节数是。
二、选择题
【3】若有说明:int i,j=7,*p=&i;则与i=j;等价的语句是()。
A.i=*p; B.*p=*&j; C.i=&j; D.i=**p;
for(i=1;i<10;i+=2)
s+=*(p+i);
printf("sum=%d",s);
}
五、编程题
【28】编写一个使用指针的C函数,交换数组a和数组b中对应元素。
【29】编写函数,通过指针连接两个字符串。
【30】产生动态数组。编写程序,输入数组大小后,通过动态分配内存函数malloc产生数组。
{*z=y-x;}
main()
{
int a,b,c;
sub(20,15,&a);
sub(a,9,&b);
sub(a,b,&c);
printf("%d,%d,%d\n",a,b,c);
}
【22】fun(int *a,int *b)
{
int *w;
*a=*a+*a;
*w=*a;
*a=*b;
*b=*w;
}
}
【8】以下程序的输出结果是( )。
A.100B.50C. 101D.200
main()
{
int *v,b;
v=&b;
b=100;
*v+=b;
printf("%d\n",b);
}
【9】以下程序的输出结果是( )。
A.2,3B. 6,2C. 3,2D. 2,6