41、找出下面程序的错误,并且解释为什么?
#include <stdio.h>
void swapxy(char*a,char*b)
{
int x = *a,y =*b;
x = x+y;
y = x-y;
x = x-y;
*a = x;
*b = y;
return;
}
int main()
{
char a = 'a',b = 'b';
char& x = a,&y = b;
printf("a = '%c' and b = '%c' before swaping.\n",a,b);
**** swapxy(x,y); ****
printf("a = '%c' and b = '%c' after swaping.\n",a,b);
}
修改后
#include <stdio.h>
void swapxy(char&a,char&b)
{
int x = a,y =b;
x = x+y;
y = x-y;
x = x-y;
a = x;
b = y;
return;
}
int main()
{
char a = 'a',b = 'B';
char& x = a,&y = b;
printf("a = '%c' and b = '%c' before swaping.\n",a,b);
swapxy(x,y);
printf("a = '%c' and b = '%c' after swaping.\n",a,b);
}
或者可以将打星号的那一行改为swapxy(&x,&y);
42、关于函数的参数在调用和返回过程中的值,下列哪个说法是正确的。
A.实参不会改变
B.实参可能会改变 √
C.如果是指针,肯定不会改变
D.如果不是指针可能会改变
43、下列代码的运行结果是什么
int *ptr;
ptr =(int*)0x8000;
*ptr = 0xaabb;
解析
运行时会报错
指针问题。这样做会导致运行的时候出错,因为这个样的写法会给一个指针分配一个随机的地址,这是非常危险的,不管这个指针有没有被使用过,这都是不允许的。
44、写出下列程序的输出结果。
#include <iostream>
using namespace std;
class A
{
public:
virtual void print(void)
{
cout <<"A::print()"<< endl;
}
};
class B : public A
{
public:
virtual void print(void)
{
cout <<"B::print()"<< endl;
}
};
class C: public A
{
public:
virtual void print(void)
{
cout <<"C::print()"<< endl;
}
};
void print(A a)
{
a.print();
}
int main(void)
{
A a,*pa,*pb,*pc;
B b;
C c;
pa = &a;
pb = &b;
pc = &c;
a.print(); //A
b.print(); //B
c.print(); //C
pa->print(); //A
pb->print(); //B
pc->print(); //C
print(a); //A
print(b); //A
print(c); //A
reutrn 0;
}
45、写出函数指针、函数返回指针,const指针,指向const的指针,指向const的cosnt指针
答案
void (*a)();
void* a();
const int* a = null;
int* const a;
const int* const a = null;
46、找出下列程序的错误并解释为什么?
#include <studio.h>
int max(int x,int y)
{
return x > y ? x : y;
}
int main()
{
int main(x,y); //错误1 一个函数中只能有一个main函数
int *p = &max; //错误2 应该声明为函数指针---int(*p)(int,int)
int a,b,c,d;
printf("Please input three interger \n");
scanf("%d%d%d",a,b,c);//错误3 应改为scanf("%d%d%d",&a,&b,&c);
d =(*p)((*p)(a,b),c); //d = a,b,c的最大值
printf("Among %d,%d,and %d,the maxmal integer is %d \n",a,b,c,d);
return 0;
}
//函数指针错误使用
#include <studio.h>
int max(int x,int y)
{
return x > y?x:y;
}
int main()
{
int main(int,int);
int (*p)(int,int) = &max;
int a,b,c,d;
printf("Please input three interger \n");
scanf("%d%d%d",&a,&b,&c);
d =(*p)((*p)(a,b),c);
printf("Among %d,%d,and %d,the maxmal integer is %d \n",a,b,c,d);
return 0;
}
47、下面数据声明都代表什么
float(**def)[10];
def是一个二级指针,它指向的是一个一位数组的指针,数组的元素都是float
double*(*gh)
[10];gh是一个指针,他指向一个一维数组,数组的元素都是double*
double(*f[10])()
;f是一个数组,f有10个元素,元素都是函数指针,指向函数类型是没有参数并且返回double的函数
int*((*b)[10])
;就跟int*(*b)[10]是一样的,b是一个一位数组的指针
Long(*fun)(int)
;函数指针
int(*(*F)(int,int))(int)
;F是一个函数指针,指向的函数的类型是有两个int参数并且返回一个函数指针的函数,返回的函数指针指向有
一个int参数且返回int的函数
48、选择题
int a[9];
int *p;
p = a;
请问哪个不能表示a[1]
A p+1,
B p++
C a++
D a+
解析
数组名a作为代表数组的首地址,是一个常量指针是不能被修改的,所以C
49、下面程序的输出结果是什么?
static int a[3][3] = {1,3,5,7,9,11,13,15,17},y,x,*p = &a[2][2];
for(x = 0;x < 3;x++)
{
y+=*(p-4*x);
}
printf("\n%d",y);
可以修改为下面的代码
static int a[3][3]={1, 3, 5,
7, 9, 11,
13, 15, 17};
static int y,x;
static int *p =&a[2][2];
for(x = 0;x < 3;x++)
{
y += *(p-4*x);
}
// 17 + 9 + 1
printf("\n%d",y);
数组指针问题。
3次循环相加的数分别是17+9+1 = 27
50、一个指向整型数组的指针的定义为:
A. int(*ptr)[]
B. int* ptr[]
C. int* (ptr[])
D. int ptr[]
答案:A
解析
int(*ptr)[] 指向整型数组的指针。
int *ptr[] 指针数组,里面存储的是地址。
int *(ptr[]),与int *ptr[]一样
int ptr[]普通数组
Tip:面对一条比较复杂的指针或引用的声明语句时,从右向左读有助于弄清它真实含义