将string的substring的C-style字符串赋给const char*变量时出现问题

哈利_蜘蛛侠 2015-11-02 11:08:33

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str("IamSpierman");
cout << str << endl;
string substr = str.substr(1);
cout << substr << endl;
const char* result = (str.substr(1)).c_str();
cout <<"\nresult == "<< result << endl;
}

为什么result的输出是空白?
...全文
141 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2015-11-05
  • 打赏
  • 举报
回复
这样试试
const char * result =new char[substr,size()+1];
strcpy(result,substr.c_str());
cout<< result<<endl;
delete result;
sdghchj 2015-11-03
  • 打赏
  • 举报
回复
引用 5 楼 akirya 的回复:
[quote=引用 4 楼 sdghchj 的回复:] [quote=引用 3 楼 akirya 的回复:]
string substr = str.substr(1);
cout << substr << endl;
const char* result = substr.c_str();
cout <<"\nresult == "<< result << endl;
这个也错了 const char* result = substr.c_str();这行之后 result也不一定有效 这样才正确
cout <<"\nresult == "<<  substr.c_str()<< endl;
无理取闹。[/quote] 呵呵,等你被这个问题坑过就不会觉得这个是无理取闹了[/quote] 行,抱歉了,请问一下,你是怎么被坑的? 请教一下, const char* result = substr.c_str(); cout <<"\nresult == "<< result << endl; 和 cout <<"\nresult == "<< substr.c_str()<< endl; 在这样同一个作用域里result为什么可能会无效?是不是某些STL实现会让c_str()返回一个野指针之类的?
赵4老师 2015-11-03
  • 打赏
  • 举报
回复
理解讨论之前请先学会如何观察! 计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构…… 对学习编程者的忠告: 多用小脑和手,少用大脑、眼睛和嘴,会更快地学会编程! 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步Debug版对应汇编一行! 单步Debug版对应汇编千行不如单步Release版对应汇编一行! 单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。 VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
  • 打赏
  • 举报
回复
引用 4 楼 sdghchj 的回复:
[quote=引用 3 楼 akirya 的回复:]
string substr = str.substr(1);
cout << substr << endl;
const char* result = substr.c_str();
cout <<"\nresult == "<< result << endl;
这个也错了 const char* result = substr.c_str();这行之后 result也不一定有效 这样才正确
cout <<"\nresult == "<<  substr.c_str()<< endl;
无理取闹。[/quote] 呵呵,等你被这个问题坑过就不会觉得这个是无理取闹了
sdghchj 2015-11-03
  • 打赏
  • 举报
回复
引用 3 楼 akirya 的回复:
string substr = str.substr(1);
cout << substr << endl;
const char* result = substr.c_str();
cout <<"\nresult == "<< result << endl;
这个也错了 const char* result = substr.c_str();这行之后 result也不一定有效 这样才正确
cout <<"\nresult == "<<  substr.c_str()<< endl;
无理取闹。
  • 打赏
  • 举报
回复
string substr = str.substr(1);
cout << substr << endl;
const char* result = substr.c_str();
cout <<"\nresult == "<< result << endl;
这个也错了 const char* result = substr.c_str();这行之后 result也不一定有效 这样才正确
cout <<"\nresult == "<<  substr.c_str()<< endl;
Mephistof 2015-11-03
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    string str("IamSpierman");
    cout << str << endl; 
    string substr = str.substr(1);
    cout << substr << endl;
    const char* result = (str.substr(1)).c_str();
    /*猜想可能是由于result只是一个指针,没有分配空间,而(str.substr(1))只是一个临时数值,在赋值语句之后就释放掉了。
可以将(str.substr(1)).c_str()改为 substr.c_str()或者是
	char result[64] = { 0 };
	strcpy(result , str.substr(1).c_str());
*/
    cout <<"\nresult == "<< result << endl;
}
sdghchj 2015-11-03
  • 打赏
  • 举报
回复
在C++里,很多函数返回的都是临时对象,不像c里内部常用一些静态存储保存字符串返回指针给你。 const char* result = (str.substr(1)).c_str(); 此处substr是临时返回的一个string对象,过了该行后对象就销毁,之后的result就是一个不确定指针。 这样才是对的: string substr = str.substr(1); cout << substr << endl; const char* result = substr.c_str(); cout <<"\nresult == "<< result << endl;

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧