#include <stdio.h> #include <stdlib.h> #include "point.h" int main() { Point hoge; hoge = point_init(0,0); printf("%d %d\n", *(hoge.x), *(hoge.y)); return 0; }
#include <stdio.h> #include "point.h" Point point_init(int s, int t) { Point p; p.x = &s; p.y = &t; int i = 1; p.flag = &i; return p; }
これはうまくいく。
しかし、
#include <stdio.h> #include "point.h" Point point_init(int s, int t) { Point hoge, *p; p = &hoge; ((*p).x) = &s; //p.y = &t; //int i = 1; //p.flag = &i; return *p; }
こうするとコンパイルは通るが、セグフォする。
さっきから考えてるんだけど理由が全然わからない。
理由がわかったら追記したい。(もし分かる人がいたら誰か教えてください)