Skip to main content

Timeline for answer to how to convert string to const char by Mysticial

Current License: CC BY-SA 3.0

Post Revisions

8 events
when toggle format what by license comment
Oct 30, 2011 at 3:07 vote accept user618879
Oct 30, 2011 at 3:00 comment added smerlin you can either store the std::string returned by gettitle() in a named std::string variable, or pass the const char* returned by c_str() directly to strcmp. If gettitle() returns a std::string& or a const std::string&, the old code was fine.
Oct 30, 2011 at 3:00 comment added Mysticial The reason is this: If a temporary is returned. It is destroyed after the statement ends. At which the pointer returned by c_str() is no longer valid. I have updated my answer to address this issue.
Oct 30, 2011 at 2:58 history edited Mysticial CC BY-SA 3.0
added 200 characters in body
Oct 30, 2011 at 2:57 comment added Mysticial @smerlin: Yes, that is a possibility. I'll fix my answer to address that.
Oct 30, 2011 at 2:56 comment added user618879 thanks that took care of that part. didnt know i needed to put .c_str() at the end
Oct 30, 2011 at 2:56 comment added smerlin u dont know if gettitle() returns a temporal string object, if that's the case, this code will yield undefined behaviour when trying to use c_str
Oct 30, 2011 at 2:53 history answered Mysticial CC BY-SA 3.0