T******7 发帖数: 1419 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: Trojan07 (usc), 信区: Programming
标 题: 面试问了个“master c++”的这个问题,答不出来,直接给枪毙了
发信站: BBS 未名空间站 (Thu Nov 27 00:49:52 2014, 美东)
int i = 42;
int *p;
int *&r = p;
r = &i;
*r = 0;
explain it line by line and what is i now. |
b******g 发帖数: 3616 | 2 C++概念菜鸟,试着回答下,错了勿鄙视-_-!
1. 定义一个整数变量i = 42
2. 定义一个整数指针p
3. 定义一个整数指针的引用r = p,这时r和p应该就是一回事了。
4. 将整数变量i的地址赋值给r,所以此时p和r的值都应该是i的地址
5. 将r所指向的整数变量(即i)的值赋值为0
所以此时 i = 0? |
T******7 发帖数: 1419 | 3 yeah you are totally right.
but lots new grads could not answer this question smoothly.
no wonder they don't have job.
【在 b******g 的大作中提到】 : C++概念菜鸟,试着回答下,错了勿鄙视-_-! : 1. 定义一个整数变量i = 42 : 2. 定义一个整数指针p : 3. 定义一个整数指针的引用r = p,这时r和p应该就是一回事了。 : 4. 将整数变量i的地址赋值给r,所以此时p和r的值都应该是i的地址 : 5. 将r所指向的整数变量(即i)的值赋值为0 : 所以此时 i = 0?
|
b******g 发帖数: 3616 | 4 惭愧惭愧。一两个月前也属于你说的那种new grads....虽然其实都还没grad...
特别是int *&r = p 这种表述一看就晕了。。。。刷LC和面试时都被指针引用做参数的
函数虐过。回头复习了下教科书,才知道这种表述要从右向左读,先是一个引用,然后
是一个对指针的引用,最后是一个对整数指针的引用-_-!
【在 T******7 的大作中提到】 : yeah you are totally right. : but lots new grads could not answer this question smoothly. : no wonder they don't have job.
|
g*********e 发帖数: 14401 | 5 new grad 面试七八成用java 就别问这些问题了 |
f***c 发帖数: 338 | 6 答对算什么水平? 俺正好在看c++ primer,碰巧这个俺也答对了。
【在 T******7 的大作中提到】 : yeah you are totally right. : but lots new grads could not answer this question smoothly. : no wonder they don't have job.
|
e*******s 发帖数: 1979 | 7 小学一年级
随便举2个
int (*(*func)(int *p))[];
int (*func)(int *p, int (*f)(int*));
写2函数操作这个指针
【在 f***c 的大作中提到】 : 答对算什么水平? 俺正好在看c++ primer,碰巧这个俺也答对了。
|
u****x 发帖数: 97 | 8 typedef int (*PARR) [N];
PARR (*pf)(int *p);
【在 e*******s 的大作中提到】 : 小学一年级 : 随便举2个 : int (*(*func)(int *p))[]; : int (*func)(int *p, int (*f)(int*)); : 写2函数操作这个指针
|
s**x 发帖数: 7506 | 9 这个对老手也挺难的,因为pointer reference 语法比较怪异,平时很少用到,一般都
直接用pointer of pointer 了。 |
k*******a 发帖数: 433 | 10 PARR应该是指向整形数组的指针;
pf应该是指向函数的指针,这个函数有一个指向整形的指针参数,返回类型为指向整形
数组的指针。
【在 u****x 的大作中提到】 : typedef int (*PARR) [N]; : PARR (*pf)(int *p);
|
|
|
k*******a 发帖数: 433 | 11 第一个:func是个指向函数的指针,
这个函数有一个指向整形的指针参数,
返回类型为指向整形数组的指针;
第二个:func是个指向函数的指针,
这个函数的返回类型为整形,
有两个参数,
第一个为指向整形的指针,
第二个为指向函数的指针,这个函数有一个指向整形的指针参数,返回类型为整形。
【在 e*******s 的大作中提到】 : 小学一年级 : 随便举2个 : int (*(*func)(int *p))[]; : int (*func)(int *p, int (*f)(int*)); : 写2函数操作这个指针
|
H**********5 发帖数: 2012 | 12 这种问题对于C/C++程序员打不出来,那说不过去。
非C/C++的答不出来很正常。
【在 T******7 的大作中提到】 : yeah you are totally right. : but lots new grads could not answer this question smoothly. : no wonder they don't have job.
|
l***i 发帖数: 1309 | 13 int *&r = p;
r = &i; // can C++ reference be changed to reference another object?
【在 T******7 的大作中提到】 : yeah you are totally right. : but lots new grads could not answer this question smoothly. : no wonder they don't have job.
|
c******n 发帖数: 13 | |
c*****t 发帖数: 585 | 15 The second one is not a reference
【在 l***i 的大作中提到】 : int *&r = p; : r = &i; // can C++ reference be changed to reference another object?
|
k*******a 发帖数: 433 | 16 这个应该是让p指向i吧
【在 l***i 的大作中提到】 : int *&r = p; : r = &i; // can C++ reference be changed to reference another object?
|
b******g 发帖数: 3616 | 17 这就是C++搞的地方,地址符和引用都是用&...
【在 l***i 的大作中提到】 : int *&r = p; : r = &i; // can C++ reference be changed to reference another object?
|
s**x 发帖数: 7506 | 18
This is basic, of course not, reference can only be initialized/set once.
【在 l***i 的大作中提到】 : int *&r = p; : r = &i; // can C++ reference be changed to reference another object?
|
s**x 发帖数: 7506 | 19
【在 l***i 的大作中提到】 : int *&r = p; : r = &i; // can C++ reference be changed to reference another object?
|