Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:38:44+00:00 2026-06-10T14:38:44+00:00

In VC++, when I typecast the const char* value returned by std::string::c_str() to char*

  • 0

In VC++, when I typecast the const char* value returned by std::string::c_str() to char* and print the casted value, nothing gets printed on the screen. Here is the code snippet

#include "stdafx.h"
#include <string>

using namespace std;

string test() { return(string("HELLO"));}


int _tmain(int argc, _TCHAR* argv[])
{
  char* val;
  val = (char*) test().c_str();
  printf("\n %s\n", val);
  return 0;
}

When I simply check the ASCII value at val[0] it is 0. But under G++, the text HELLO gets displayed.

Is the cast from const char* to char* a non-standard one whose results are not defined?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:38:46+00:00Added an answer on June 10, 2026 at 2:38 pm

    The cast is fine as long as you don’t modify the value or read from it after altering the string (you should get at least a warning though).

    The problem is that you call the function on a temporary (the temporary is the string returned by cat()). After the ;, the temporary goes out of scope and val still points to memory managed by the temp. It’s UB reading from it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Convert.ToInt32(ddlBuyer1Country.SelectedValue) ; to typecast a string returned by selectedvalue. But this
I have a mmap typecast to a char pointer char *ptr; ptr = (char
Is it possible to convert or typecast a char array gg[16][16] to an object
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
here is the code for my question: class ICommon { public: virtual ICommon& operator=(const
I am trying to create my own std::string wrapper to extend its functionality. But
I've stored a pointer to a type_info object. int MyVariable = 123; const std::type_info*
When you typecast from an int to a char, you are cutting down the
I am observing an interesting result when I typecast an output: Here is the
I was wondering what is the best way to typecast a value from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.