C++复制指针

1 c++ pointers structure

我在弄清楚复制指针的语法时遇到了麻烦.

我该怎么做theVar2=theVar

Struct MyStructureType {
    double* theVar2;
}

MyStructureType* myStruct;
double* theVar;

theVar = malloc(sizeof(double));


myStruct->theVar2 = theVar; //segfaults
Run Code Online (Sandbox Code Playgroud)

Pra*_*een 5

拳头为内存分配MyStructureType,然后使用data member其中.

MyStructureType* myStruct = new MyStructureType();
double* theVar = new double();
myStruct->theVar2 = theVar;
Run Code Online (Sandbox Code Playgroud)