从函数返回C风格的字符串? [英] Return C-style string from function?

查看:82
本文介绍了从函数返回C风格的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>


使用命名空间std;


const char * test(char * src){


string s(src);


返回s.c_str();

}


int main(){


char src [] =" test123";


cout<< test( src);


返回0;

};


如何获得test123从功能?


谢谢。

#include <iostream>

using namespace std;

const char* test(char *src) {

string s(src);

return s.c_str();
}

int main() {

char src[] = "test123";

cout<<test(src);

return 0;
};

how to get the "test123" from the function?

thanks.

推荐答案



怎么样 < ho ****** @ gmail.com在留言中写道

news:11 ********************* @ b28g2000cwb。 googlegro ups.com ...

"howa" <ho******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...

#include< iostream>


using namespace std;


const char * test(char * src){


string s(src);


返回s。 c_str();

}


int main(){


char src [] =" test123" ;;


cout<< test(src);


返回0;

};


如何获得test123从功能?


谢谢。
#include <iostream>

using namespace std;

const char* test(char *src) {

string s(src);

return s.c_str();
}

int main() {

char src[] = "test123";

cout<<test(src);

return 0;
};

how to get the "test123" from the function?

thanks.



当函数完成后,字符串s超出范围,

和,因为它在堆栈中,它变成垃圾。


快速解决方法是更改​​线路

返回s.c_str() ;



返回strdup(s.c_str());


Serafeim

When the function finishes the string s goes out of scope,
and, since its in the stack, it becomes garbage.

A quick fix is to change the line
return s.c_str();
to
return strdup(s.c_str() );

Serafeim

< br>

在文章< 11 ********************* @ b28g2000cwb.googlegroups中。 com>,

" howa" < ho ****** @ gmail.comwrote:
In article <11*********************@b28g2000cwb.googlegroups. com>,
"howa" <ho******@gmail.comwrote:

#include< iostream>


using namespace std;


const char * test(char * src){


string s(src);


返回s.c_str();

}


int main(){


char src [] =" test123";


cout<< test(src);


返回0;

};


如何获得test123从功能?


谢谢。
#include <iostream>

using namespace std;

const char* test(char *src) {

string s(src);

return s.c_str();
}

int main() {

char src[] = "test123";

cout<<test(src);

return 0;
};

how to get the "test123" from the function?

thanks.



const char * test(char * src){

返回src;

}


-

要给我发电子邮件,请输入sheltie在主题中。

const char* test( char* src ) {
return src;
}

--
To send me email, put "sheltie" in the subject.




Papastefanos Serafeim?ˉ?é?????

Papastefanos Serafeim ?ˉ?é?????

" howa" < ho ****** @ gmail.com在留言中写道

news:11 ********************* @ b28g2000cwb。 googlegro ups.com ...
"howa" <ho******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...

#include< iostream>


using namespace std;


const char * test(char * src){


string s(src);


返回s。 c_str();

}


int main(){


char src [] =" test123" ;;


cout<< test(src);


返回0;

};


如何获得test123从功能?


谢谢。
#include <iostream>

using namespace std;

const char* test(char *src) {

string s(src);

return s.c_str();
}

int main() {

char src[] = "test123";

cout<<test(src);

return 0;
};

how to get the "test123" from the function?

thanks.



当函数完成时,字符串s超出范围,

,并且,因为它在堆栈中,它变成垃圾。 />

快速解决方法是更改​​行

返回s.c_str();



return strdup(s.c_str());


Serafeim


When the function finishes the string s goes out of scope,
and, since its in the stack, it becomes garbage.

A quick fix is to change the line
return s.c_str();
to
return strdup(s.c_str() );

Serafeim



谢谢...

这是从函数中传递字符串的通常/正常方式吗?

Thanks...

is this a usual/normal way to pass a string out of a function?


这篇关于从函数返回C风格的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆