私は、C プロジェクトに戻るたびに、これをやめます。構造内の構造にアクセスしようとすると、segfault が発生します。ゲームに次の(単純化された)構造があるとしましょう:
struct vector {
float x;
float y;
};
struct ship {
struct vector *position;
};
struct game {
struct ship *ship;
} game;
そして、船を初期化する関数:
static void
create_ship(struct ship *ship)
{
ship = malloc(sizeof(struct ship));
ship->position = malloc(sizeof(struct vector));
ship->position->x = 10.0;
}
次に、main() で下に移動します。
int main() {
create_ship(game.ship);
printf("%f\n", game.ship->position->x); // <-- SEGFAULT
}