【发布时间】:2016-02-26 05:13:41
【问题描述】:
我无法弄清楚复制指针的语法。
我将如何制作theVar2=theVar?
Struct MyStructureType {
double* theVar2;
}
MyStructureType* myStruct;
double* theVar;
theVar = malloc(sizeof(double));
myStruct->theVar2 = theVar; //segfaults
【问题讨论】:
-
为什么在 C++ 中使用
malloc()而不是new? -
请发帖MCVE。您确定在实际代码中分配了一些有效的缓冲区并将其地址分配给
myStruct? -
在 C++ 中 a->b 表示 (*a).b,因此这里由于 myStruct 未初始化,因此会出现段错误。