Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:11:51+00:00 2026-06-12T07:11:51+00:00

I dork this up just about every time I jump back into a C

  • 0

I dork this up just about every time I jump back into a C project. I’m getting a segfault when attempting to access a structure within a structure. Let’s say I have the following (simplified) structures for a game:

struct vector {
    float x;
    float y;
};

struct ship {
    struct vector *position;
};

struct game {
    struct ship *ship;
} game;

And a function to initialize the ship:

static void
create_ship(struct ship *ship)
{
    ship = malloc(sizeof(struct ship));
    ship->position = malloc(sizeof(struct vector));
    ship->position->x = 10.0;
}

Then down in main():

int main() {
    create_ship(game.ship);
    printf("%f\n", game.ship->position->x); // <-- SEGFAULT
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:11:52+00:00Added an answer on June 12, 2026 at 7:11 am

    You are passing game.ship by value, so inside create_ship the variable ship is just a copy of that pointer, and it just changes the copy. When the function returns, the pointer to what you malloced is lost and the effects of the function are not visible outside it, except for the memory leak.

    You need to pass a pointer to the pointer and modify game.ship through that:

    static void
    create_ship(struct ship **ship)
    {
        *ship = malloc(sizeof(struct ship));
        (*ship)->position = malloc(sizeof(struct vector));
        (*ship)->position->x = 10.0;
    }
    

    And

    create_ship(&game.ship);
    

    Or perhaps a better way would be to do as Johnny Mopp suggests in the comments, return the pointer from the function instead of modifying one outside of it:

    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;
    }
    

    And

    game.ship = create_ship();
    

    And of course, what you have malloced, don’t forget to free.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like a dork just for asking this, but I'm not getting any
This is not that important, but I'm getting conflicting information about this so I
I'm the kind of dork who enjoys reading source code in his spare time.
I'm having a problem with JQGrid using ASP.NET MVC. I'm trying to follow this
To maintain my purity and honor as a database dork, I wish to update
I have the following situation: On[t::shdw] Message[t::shdw,t,Foobar,Dork] -> t::shdw: Symbol t appears in multiple
To my surprise, I can't do anything nearly as simple as this, from what
I have an array $product_urls containing 200 elements. Problem: When I do a print_r($product_urls)
I've followed a number of detailed tutorials on how to use a COM object
Let's say I create these two methods: public void AddScriptToPage(params string[] scripts) { /*...*/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.