我正在创建一个指向结构的指针,以在C中用malloc创建一个动态数组,但是我得到了调用结构数组的段错误。这里是我的代码的简要介绍:指向指向结构的指针和指向数组的malloc内存的指针
#include <stdio.h>
typedef struct {
int test1;
int test2;
}testStruct;
int main() {
testStruct **neato;
neato = (testStruct **) malloc(sizeof(testStruct *) * 5);
// Array of 5 for convience
// any neato[x]->testy call results in segmentation fault.
scanf("%d", &neato[0]->test1); // Segmentation fault
return 0;
}
我试过其他类似的调用(* NEATO)[0] .test1和所有导致分段错误。这显然不是这样做的正确方法,或者我的GNU编译器严重过时。
不要投malloc'的'的返回值。请参阅:http://c-faq.com/malloc/mallocnocast.html – 2012-04-05 02:44:19