面试题:指针

一、指针

1、利用指针实现两数交换:

#include <iostream>
using namespace std;

void swap1(int p,int q){
	int t;
	t=p;
	p=q;
	q=t;
}
void swap2(int *p,int *q){
	int *t;
	*t=*p;
	*p=*q;
	*q=*t;
}
void swap3(int *p,int *q){
	int *t;
	t=p;
	p=q;
	q=t;
}
void swap4(int *p,int *q){
	int t;
	t=*p;
	*p=*q;
	*q=t;
}
void swap5(int &p,int &q){
	int t;
	t=p;
	p=q;
	q=t;
}
int main(){
	int a=1,b=2;
	swap4(&a,&b);
	swap5(a,b);
	cout<<a<<" "<<b;
    return 0;
}

其中只有swap4和swap5是可以实现交换的

对于swap2:先是 int *t,声明一个int指针,但是没有地址,就直接*t=*p,将p地址的内容赋值给t地址,但是t没有地址,这在vs中是编译不通过的

对于swap3:虽然p地址的内容确实是a,但是p指针是个局部变量,swap3只是将p、q两个指针进行了交换,无法达到交换a,b的目的


2、指出以下程序的错误所在:

#include <iostream>
using namespace std;

void GetMemory(char *p,int num){
	p=(char *)malloc(sizeof(char)*num);
}
int main(){
	char *str=NULL;
	GetMemory(str,100);
	strcpy(str,"hello");
	cout<<str;
    return 0;
}

问题出在GetMemory上,*p实际上是str的一个副本,p申请的内存和str无关

可以利用指向指针的指针方式修改此题:

#include <iostream>
using namespace std;

void GetMemory(char **p,int num){
	*p=(char *)malloc(sizeof(char)*num);
}
int main(){
	char *str=NULL;
	GetMemory(&str,100);
	strcpy(str,"hello");
	cout<<str;
    return 0;
}

也可以将p地址利用返回值赋给str:

#include <iostream>
using namespace std;

char * GetMemory(char *p,int num){
	p=(char *)malloc(sizeof(char)*num);
	return p;
}
int main(){
	char *str=NULL;
	str=GetMemory(str,100);
	strcpy(str,"hello");
	cout<<str;
    return 0;
}

3、指出以下程序的错误之处:

#include <iostream>
using namespace std;

char * strA(){
	char str[]="hello world";
	return str;
}
int main(){
	
	cout<<strA();
    return 0;
}

这里考核的是数组与指针的区别,数组分配的是一个局部数组,指针分配的是一个全局数组,所以strA执行完后str数组是被释放掉的,所以正确书写如下:

#include <iostream>
using namespace std;

char * strA(){
	char *str="hello world";
	return str;
}
int main(){
	
	cout<<strA();
    return 0;
}

还可以这么写:

#include <iostream>
using namespace std;

char * strA(){
	static char str[]="hello world";
	return str;
}
int main(){
	
	cout<<strA();
    return 0;
}

4、下列程序哪一行崩溃:

#include <iostream>
using namespace std;

struct S{
	int i;
	int *p;
};
int main(){
	S s;
	int *p=&s.i;
	p[0]=4;
	p[1]=3;
	s.p=p;
	s.p[1]=1;
	s.p[0]=2;
	//cout<<s.p;
    return 0;
}

一行一行分析:

p指针指向s.i的地址,p[0]=4,p[1]=3,所以此时s.i为4,s.p为3(地址为3)

s.p=p,即s.p=&s.i,s.p[1]也就是s.p,是同一个地方,此时s.p地址为1

最后,s.p[0],因为s.p地址变了,所以就不是s.i了,相当于*((int *)1)=2;访问的是0x00000001的空间——对一个未做声明的地址进行访问,访问出错


二、函数指针

1、写出函数指针、函数返回指针、const指针、指向const指针、指向const的const指针

void (*f)()
void * f()
const int*
int * const
const int* const

2、下面的数据声明都代表什么?

float(**def)[10];//def是一个二级指针,它指向的是一个一维数组的指针,数组元素都是float
double*(*gh)[10];//gh是一个指针,它指向一个一维数组,数组元素都是double*
double(*f[10])();//f是一个数组,有10个元素,元素都是函数的指针,指向的函数类型是没有参数且返回double的函数
int*((*b)[10]);//就跟int*(*b)[10]是一样的,是一维数组的指针
long(*fun)(int);//函数指针,参数int返回值long
int(*(*f)(int,int))(int);//f是一个函数的指针,指向的函数类型是两个int参数且返回一个函数指针的函数,返回的函数指针指向一个有一个int参数且返回int的函数


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值