#include <stdio.h>
#include <stdlib.h>
struct Child {
};
struct Parent {
struct Child *child;
};
int main(void)
{
struct Child *child = (struct Child *)malloc(sizeof(struct Child));
struct Parent *parent;
parent->child = child;
return 0;
}
报错为:bash: line 1: 30009 Segmentation fault: 11 "~/Code/hello_c/main",但是如果把这两行交换,就不会报错,如下:
struct Parent *parent;
struct Child *child = (struct Child *)malloc(sizeof(struct Child));
想不通是为啥,特来请教。