当前位置:文档之家› 2-指针与引用练习题

2-指针与引用练习题

C++程序设计课件 设计制作:徐龙琴 16
改后的运行结果为: 运行结果为: a[0]=0 a[0]=0 a[1]=1 a[1]=1 a[2]=4 a[2]=4 a[3]=9 a[3]=9 a[4]=16 a[4]=16 a[5]=25 a[5]=25 a[6]=36 a[6]=36 a[7]=49 a[7]=49 a[8]=64 a[8]=64 a[9]=81 a[9]=81 *a=0 a=0x0065FDCC
//p 为指针数组
int (*p)[n]; //p 为指向数组的指针变量 int *p( );
//p 为返回指针型的函数
int (*p) ( ); //p 为函数指针
int **p;
//p 为指向指针的指针变量
C++程序设计课件
设计制作:徐龙琴
8
4 指出下列程序的错误。 #include <iostream.h> void exchange(int,int); void exchange(int*,int*); exchange(int&,int&); void main() void main() {cout<<"Input two data seprated by space:"; {cout<<"Input two data seprated by space:"; int a,b; int a,b; int a,b,*p1=&a,*p2=&b; cin>>a>>b; cout<<"Before Exchange:a="<<a cin>>a>>b; cin>>a>>b; <<",b="<<b<<endl; cout<<"Before Exchange:a="<<a<<",b="<<b<<endl; cout<<"Before Exchange:a="<<a<<",b="<<b<<endl; exchange(a,b); exchange(a,b); exchange(p1,p2); cout<<"After Exchange:a= "<<a cout<<"After Exchange:a="<<a<<",b="<<b<<endl; cout<<"After Exchange:a="<<a<<",b="<<b<<endl; <<",b="<<b<<endl; }} void exchange(int x,int y)&y) &x,int *x,int *y) { int t; {int t;t; { int t=x;x=y;y=t; t=x;x=y;y=t; t=*x;*x=*y;*y=t; } } }
运行结果为: 12 6
C++程序设计课件
设计制作:徐龙琴
15
7 阅读程序,给出运行结果。 #include<iostream.h> void main() { int i,a[10]; int *p1,*p2; p1=a; p1=&a[0]; p2=p1+5; for (i=0;i<=9;i++) {a[i]=i*i; p1= 0x0065FDCC,p2= 0x0065FDED p1= 0x0065FDCC,p2= 0x0065FDED cout<<"a["<<i<<"]="<<a[i]<<endl;} p2-p1=5 p2-p1=5 *p1-*p2=25 25 a[5]-a[0]= cout<<"*a="<<*a <<endl; cout<<"a="<<a<<endl; cout<<"p1="<<p1<<",p2="<<p2<<endl; cout<<"p2-p1="<<p2-p1<<endl; cout<<"*p2-*p1="<<*p2-*p1<<endl; cout<<"a[5]-a[0]="<< a[5]-a[0] <<endl; }
a
{ int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a; printf("%d", *(p+2)); } a. 3 b. 4 c. 1 d. 2
设计制作:徐龙琴 1
C++程序设计课件
3、若0<i<10,则对数组元素地址的正确表示是: d int a[]={1,2,3,4,5,6,7,8,9,0},*p,i; p=a; a.&(a+1) c.*p b.a++ d.&p[i]
p=&a[0];
x=*(p+2); y=*(p+4); printf("*p=%d,x=%d,y=%d\n",*p,x,y); return; } 运行结果为: *p=1,x=3,y=5
C++程序设计课件
设计制作:徐龙琴
11
3
#include<iostream.h> void callbyval(int a,int b,int c) {a=3;b=2;c=1;} void callbypointer(int* a,int* b,int* c) {*a=3;*b=2;*c=1;} void callbyreference(int& a,int& b,int& c) { a=1;b=2;c=3;} void main() { int a=1,b=2,c=3; int& a1=a; int& b1=a; int& c1=a; callbyval(a,b,c); cout<<a<<b<<c<<endl; callbypointer(&a,&b,&c); cout<<a<<b<<c<<endl; callbyreference(a1,b1,c1); cout<<a<<b<<c<<endl;}
B)相加 D)赋值
运算是没有意义的。
13、已知指针p的指向下图所示,则表达式*--p的值是: 20
a[0]在低地址,a[1]在高地址
C++程序设计课件
设计制作:徐龙琴
5
二 下列程序有无错误: 1、 #include <iostream.h> int fun1(int); int fun2(int); void main() { int i=12; int (*fp)(); int (*fp)(int); fp = fun1; cout<<"Fun1:"<<(*fp)(i)<<endl; fp = fun2; cout<<"Fun2:"<<(*fp)(i)<<endl;} int fun1(int k) { k = k*2+1; return k;} int fun2(int m) { m = m/2+1; return m;}
C++程序设计课件
运行结果为: 10,0 10,0 21,31 21,31 22,32 22,32
14
设计制作:徐龙琴
6
#include <iostream.h> void main() { int a[2][3]={1,2,3,4,5,6}; int i,j,(*p)[3]; p=a; cout<<"input i,j:"; cin>>i>>j; cout<<"a["<<i<<","<<j<<"]= " <<*(*(p+i)+j)<<endl; }
9、设有以下函数定义,则该函数返回的值是 a int * fun(int a) {int *t,n; n=a;t=&n; return t;} a.一个不可用的存储单元地址值 b.一个可用的存储单元地址值 c.n中的值 d.形参a中的值 10、下面函数的功能是: a int fun(char *x) {char *y=x; while(*y++){}; return y-x-1;} a. 求字符串的长度 b. 求字符串存放位置 c. 比较两个字符串的大小 d. 将字符串x连接到字符串y后面
设计制作:徐龙琴
C++程序设计课件
4
11、用new为int数组分配10个存储空间,下面哪个语句正确 B A) int *p=new; C) int *p=new int[]; A)相减 C)比较 B) int *pint;
相关主题