2014-05-14 28 views
1

我嘗試轉換的wstring爲const WCHAR *,但空終端不工作,我想到:wstring的爲const WCHAR *使用c_str()

index 4

index 5

code

是否有任何方法可以將wstring轉換爲const wchar *的方式,我正在嘗試?

+0

的可能重複[我要的std :: string轉換爲一個const WCHAR \ _t \ *](http://stackoverflow.com/questions/246806/i-want-to-convert-stdstring- into-a-const-wchar-t) – 101010

+0

你有沒有試過const_cast? – Beed

+0

@Beed,'c_str()'返回'const wchar_t *'。它存儲在一個'const wchar_t *'中。爲什麼'const_cast'會有幫助? – chris

回答

3

works[5]doesntWork[5]都會導致未定義的行爲。緩衝區僅在[4]之間有效,而[5]不在此範圍內。

const wchar_t *works = L"Test"; 

works[0] -> L'T' 
works[1] -> L'e' 
works[2] -> L's' 
works[3] -> L't' 
works[4] -> L'\0' 
works[5] -> undefined behavior, the program is broken if it ever tries to access this 
相關問題