- C++相談室 part134
162 :デフォルトの名無しさん (マクド FF6b-xgEu)[sage]:2018/02/17(土) 09:43:48.64 ID:Lqib7/b9F - 以下のようにconst char*型の文字列でchar*型の変数を初期化したいときに
strcpyやstrlenを書かずにstd::stringを利用して短くかけないかと思うのですが msvcだとエラーにならず、clang, gccだとdeleteでセグメンテーション違反になります。 どうすればより良い感じになるでしょうか?const_castはできれば使いたくないです。 #include <string.h> #include <string> #include <iostream> int main() { const char* asdf = "asdf"; // char* psz2 = new char[strlen(asdf)+1]; // strcpy(psz2, asdf); std::string *psz = new std::string(asdf); char* psz2 = &(*psz)[0]; std::cout << psz2 << std::endl; delete psz2; return 0; }
|
|