#include <stdio.h> #include <stdlib.h> struct students{ int age; int high; int *p; }; void init(struct students* s ) { s->age=10; s->high=188; s->p=(int*)malloc(sizeof(int*)); } int main() { struct students* tom; init(tom); printf("tom.age=%d,tome.high=%d,tom.p=%p\n",tom->age,tom->high,tom->p); return 0; }
编译代码会出现段错误!!!
代码修改如下:
#include <stdio.h> #include <stdlib.h> struct students{ int age; int high; int *p; }; void init(struct students* s ) { s->age=10; s->high=188; s->p=(int*)malloc(sizeof(int*)); } int main() { struct students tom; init(&tom); printf("tom.age=%d,tome.high=%d,tom->p=%p\n",tom.age,tom.high,tom.p); return 0; }
编译正常!!!