avatar
bloomberg电面面经# JobHunting - 待字闺中
c*e
1
1.怎么写char* func(void),考return by pointer to local variable.
avatar
d*o
2
得用static variable吧,不然无法返回
avatar
p*2
3

new一下就可以了吧?

【在 d***o 的大作中提到】
: 得用static variable吧,不然无法返回
avatar
h*e
4
new is fine
avatar
c*e
5
Do you mean the following? Thanks.
char* func(void) {
return new char;
}

【在 h********e 的大作中提到】
: new is fine
avatar
c*e
6
It looks f1() is OK, but f2 is not.
Is my understanding correct? Thanks.
char* f1() {
char* p=new char;
*p='x';
return p;
}
char* f2() {
char ch='a';
char* p=&ch;
return p;
}

【在 h********e 的大作中提到】
: new is fine
avatar
p*e
7
f2() the pointer pointed to the local variable

【在 c**********e 的大作中提到】
: It looks f1() is OK, but f2 is not.
: Is my understanding correct? Thanks.
: char* f1() {
: char* p=new char;
: *p='x';
: return p;
: }
: char* f2() {
: char ch='a';
: char* p=&ch;

avatar
h*e
8
local variable只会放在stack里面。。函数调用结束就没了。所以不能返回。new都是
在堆里面。。
avatar
s*r
9
....
avatar
A*u
10
这个应该靠const char* 返回,更难一点
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。