关于memcpy处理string的问题
为什么第一个程序用memcpy拷贝string后,p不为空,但指向的是空值。而第二个程序拷贝int类型的p指向的不为空?
第一个程序:
#include<iostream>#include<string>using namespace std;int main(){ string aaa = "我是谁呀?"; int len = sizeof(aaa); char *p = new char[len]; memcpy(p ,&aaa, len); cout<<p<<endl; system("pause"); return 0;}
#include<iostream>#include<string>using namespace std;int main(){ int i = 333; int len = sizeof(i); char *p = new char[len]; memcpy(p ,&i, len); cout<<p<<endl; system("pause"); return 0;}
int _tmain(int argc, _TCHAR* argv[]){ char *aaa = "我是谁呀?"; int len = strlen(aaa); char *p = new char[len + 1]; p[len] = '\0'; memcpy(p ,aaa, len); cout<<p<<endl; system("pause"); return 0;}