Guest User

Untitled

a guest
Jan 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. struct vector {
  2. float x;
  3. float y;
  4. };
  5.  
  6. struct ship {
  7. struct vector *position;
  8. };
  9.  
  10. struct game {
  11. struct ship *ship;
  12. } game;
  13.  
  14. static void
  15. create_ship(struct ship *ship)
  16. {
  17. ship = malloc(sizeof(struct ship));
  18. ship->position = malloc(sizeof(struct vector));
  19. ship->position->x = 10.0;
  20. }
  21.  
  22. int main() {
  23. create_ship(game.ship);
  24. printf("%fn", game.ship->position->x); // <-- SEGFAULT
  25. }
  26.  
  27. static void
  28. create_ship(struct ship **ship)
  29. {
  30. *ship = malloc(sizeof(struct ship));
  31. (*ship)->position = malloc(sizeof(struct vector));
  32. (*ship)->position->x = 10.0;
  33. }
  34.  
  35. create_ship(&game.ship);
  36.  
  37. static struct ship* create_ship()
  38. {
  39. struct ship* s = malloc(sizeof(struct ship));
  40. s->position = malloc(sizeof(struct vector));
  41. s->position->x = 10.0;
  42.  
  43. return s;
  44. }
  45.  
  46. game.ship = create_ship();
  47.  
  48. static void create_ship(struct ship **ship);
  49.  
  50. create_ship(&game.ship);
  51.  
  52. static void create_ship(struct ship **ship)
  53. {
  54. *ship = malloc(sizeof(struct ship));
  55. // ...
  56.  
  57. static ship *create_ship()
  58. {
  59. struct ship *ship = malloc(sizeof(struct ship));
  60. // ...
  61.  
  62. return ship;
  63. }
  64.  
  65. // ...
  66.  
  67. game.ship = create_ship();
Add Comment
Please, Sign In to add comment