Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- int main() {
- create_ship(game.ship);
- printf("%fn", game.ship->position->x); // <-- SEGFAULT
- }
- static void
- create_ship(struct ship **ship)
- {
- *ship = malloc(sizeof(struct ship));
- (*ship)->position = malloc(sizeof(struct vector));
- (*ship)->position->x = 10.0;
- }
- create_ship(&game.ship);
- static struct ship* create_ship()
- {
- struct ship* s = malloc(sizeof(struct ship));
- s->position = malloc(sizeof(struct vector));
- s->position->x = 10.0;
- return s;
- }
- game.ship = create_ship();
- static void create_ship(struct ship **ship);
- create_ship(&game.ship);
- static void create_ship(struct ship **ship)
- {
- *ship = malloc(sizeof(struct ship));
- // ...
- static ship *create_ship()
- {
- struct ship *ship = malloc(sizeof(struct ship));
- // ...
- return ship;
- }
- // ...
- game.ship = create_ship();
Add Comment
Please, Sign In to add comment