2012-01-22 15 views

答えて

3

あなたは単にあなたがchar*なくconst char *に文字列データを取得する必要がある場合、あなたはそのようにcopyする必要がstrings::c_str()

string final; 
const char *pp = final.c_str(); 

を使用する必要があります。

std::string final; 

//Allocate pointer large enough to hold the string data + NULL 
char *pp = new char[final.size() + 1]; 

//Use Standard lib algorithm to copy string data to allocated buffer 
std::copy(final.begin(), final.end(), pp); 

//Null terminate after copying it 
pp[final.size()] = '\0'; 

//Make use of the char * 


//delete the allocated memory after use, notice delete [] 
delete []pp; 
1

なぜあなたは* charを持つ必要があります。私は最終的に全てを連結します。 * charが必要な場合は、電話でこれを達成できます。

final.c_str(); 

charを使用する場合は、

i=final.size()+1; 
char *pp = new char[i]; 
1

あなたが直接char*stringを変換することはできません:あなたは十分なメモリを確保していることを確認してください。

const char*が適切な場合は、string::c_str()を使用できます。

それ以外の場合は、文字列の内容を事前に割り当てられたchar配列にコピーする必要があります。

関連する問題