構造体の入れ子を関数に渡す時の注意[C言語]
入れ子の構造体はp->start.horizontal; アロー演算子の使い方に注意 #includetypedef struct{ double horizontal;//x 座標 double vertival;//y }COORDINATE; typedef struct{ COORDINATE start; COORDINATE goal; COORDINATE present; int total_time; int now_time; }PLANE; int input_data(PLANE* p); int calc_data(PLANE* p); int output_data(PLANE* p); int main(void){ PLANE p; input_data(&p); output_data(&p); return 0; } int input_data(PLANE* p){ do{ printf("Input the X coordinate of staring point >>"); scanf( "%lf", &p->start.horizontal ); }while(p->start.horizontal < 0); do{ printf("Input the Y coordinate of staring point >>"); scanf( "%lf", &p->start.vertival ); }while(p->start.vertival < 0); do{ printf("Input the X coordinate of arrival point >>"); scanf( "%lf", &p->goal.horizontal ); }while(p->goal.horizontal < 0); do{ printf("Input the Y coordinate of arrival point >>"); scanf( "%lf", &p->goal.vertival ); }while(p->goal.vertival < 0); printf("\n"); do{ printf("Input the flight time >>"); scanf( "%d", &p->total_time); }while(p->total_time < 0); do{ printf("Input the time after departure >>"); scanf( "%d", &p->now_time); }while(p->now_time < 0 || p->total_time < p->now_time); printf("\n"); calc_data(p); return 0; } int calc_data(PLANE* p){ p->present.horizontal = p->start.horizontal + ((p->goal.horizontal - p->start.horizontal)/p->total_time * p->now_time); p->present.vertival = p->start.vertival + ((p->goal.vertival - p->start.vertival)/p->total_time * p->now_time); return 0; } int output_data(PLANE* p){ printf("Coordinate of starting point x: %8.2lf ,y: %8.2lf \n",p->start.horizontal,p->start.vertival); printf("Coordinate of arrival point x: %8.2lf ,y: %8.2lf \n",p->goal.horizontal,p->goal.vertival); printf("Flight time %3d\n\n",p->total_time); printf("%3d minutes have passed after departure\n",p->now_time); printf("Coordinates at present point x:%8.2lf, y:%8.2lf \n",p->present.horizontal,p->present.vertival); return 0; }
>