面试题:指针
来源:互联网 发布:腾牛装b神器软件 编辑:程序博客网 时间:2024/04/29 19:02
一、指针
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 * constconst int* const
2、下面的数据声明都代表什么?
float(**def)[10];//def是一个二级指针,它指向的是一个一维数组的指针,数组元素都是floatdouble*(*gh)[10];//gh是一个指针,它指向一个一维数组,数组元素都是double*double(*f[10])();//f是一个数组,有10个元素,元素都是函数的指针,指向的函数类型是没有参数且返回double的函数int*((*b)[10]);//就跟int*(*b)[10]是一样的,是一维数组的指针long(*fun)(int);//函数指针,参数int返回值longint(*(*f)(int,int))(int);//f是一个函数的指针,指向的函数类型是两个int参数且返回一个函数指针的函数,返回的函数指针指向一个有一个int参数且返回int的函数
0 0
- 指针面试题解析
- 指针的面试题.
- 指针使用面试题
- 面试题---指针篇
- 指针面试题
- 面试题---函数指针
- 【面试题】关于指针
- 面试题:指针
- 数组指针面试题
- 指针面试题
- 阿里巴巴面试题-----指针
- C语言指针 -- 面试题
- 面试题 引用和指针
- 指针的经典面试题
- [面试题]const与指针
- 面试题:三级指针问题
- [指针八]有关指针的面试题
- IOS面试题之--C面试题指针
- Android框架浅析
- WIFI操作流程源码分析—启动
- 换新电脑了
- 联想E420电脑没有外音,耳机有声音的问题
- 疯狂ios讲义之绘制曲线
- 面试题:指针
- Android技巧拾取
- WIFI操作流程源码分析—扫描
- 学习方法理论提升
- Android webview 与 JS相互调用
- OpenNMS全接触-系统架构(三)
- 调试技巧:让断点停在for循环中i为某个值得时候
- CentOS中的中文乱码问题的解决
- LeetCode Valid Sudoku