我想要(在Fedora在GCC 4.3 11點i586的)運行下面的代碼內的結構:指針到嵌套結構
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
struct s_smallstruct{
int smallstruct;
};
struct s_test2{
char * test2;
struct s_smallstruct* smallstruct;
};
struct s_test3{
char * test3;
struct s_smallstruct * smallstruct;
};
struct s_test1{
char * test1;
struct s_test2 * test2;
struct s_test3 * test3;
};
int main(){
struct s_test1 *test1 = (struct s_test1 *) malloc(sizeof test1);
test1->test2[0].smallstruct[0].smallstruct = 123;
int num = test1->test2[0].smallstruct[0].smallstruct;
// struct s_smallstruct * smallstruct = (struct s_smallstruct *) malloc(sizeof smallstruct);
// smallstruct[0].smallstruct =12;
// int num = smallstruct[0].smallstruct;
printf("%d\n" , num);
return EXIT_SUCCESS;
}
但我拿到test1-> TEST2段錯誤[0]。 smallstruct [0] .smallstruct = 123; 。評論部分運行時沒有錯誤。這種行爲的原因是什麼?我不是很擅長C,所以我會很感激任何幫助。
這不是一個解決方案,但總是檢查* ALLOC! – 2011-04-16 20:17:00