-1

I have function that should convert char* to wchar_*. But it doesn't - I have just strange string of 3 spaces. Is that because GetWC returns pointer to not existing string?

const wchar_t *GetWC(const char *c)
{
    size_t cSize = strlen(c)+1;
    std::wstring wc( cSize, L'#');
    mbstowcs( &wc[0], c, cSize );
    return wc.c_str();
}


int _tmain(int argc, _TCHAR* argv[])
{
    char *g ="aaa";

    const wchar_t* f= GetWC(g);
    wcout<<f;

    return 0;
}
vico
  • 17,051
  • 45
  • 159
  • 315
  • Return `std::wstring` by value, to preserve its value. Anyone who really needs a pointer can call `c_str()` themselves. – Mike Seymour Sep 18 '14 at 16:03

1 Answers1

1

wc.c_str() is not valid after GetWC returns. wc is a local variable

dohashi
  • 1,771
  • 8
  • 12