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;
}
私は(*格好いい)のような他の呼び出し[0] .test1とセグメンテーションフォールトのすべての結果を試してみました。これは明らかにこれを行う適切な方法ではありません。あるいはGNUコンパイラは真剣に時代遅れです。
'malloc'の戻り値をキャストしないでください。参照:http://c-faq.com/malloc/mallocnocast.html –