我正在做一些维护工作,遇到类似以下的事情:
std::string s;
s.resize( strLength );
// strLength is a size_t with the length of a C string in it.
memcpy( &s[0], str, strLength );
Run Code Online (Sandbox Code Playgroud)
我知道使用&s [0]如果它是std :: vector会是安全的,但是这是std :: string的安全使用吗?
为什么以下作业不起作用?如果可能的话,我想要一个低级的解释。另外,这是我得到的编译器错误:'char*' 到 'char [20]' 赋值中的类型不兼容
class UCSDStudent {
char name[20];
public:
UCSDStudent( char name[] ) {
//this-> name = name; does not work! Please explain why not
strcopy( this -> copy, copy ); //works
}
};
Run Code Online (Sandbox Code Playgroud)