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

  • Home
  • SEARCH
  • 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 6032427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:19:57+00:00 2026-05-23T05:19:57+00:00

I am trying to allocate an array of structs within a function. My struct

  • 0

I am trying to allocate an array of structs within a function.

My struct is as follows:

 typedef struct{
      uint16_t  taskNumber;
      uint16_t  taskType;          
      double    lat;               
      double    lon;               
      double    speed;             
      uint8_t   successCriteria;   
      uint16_t  successValue;      
      uint8_t   nextPoint;      
 }missionPoint;

In my code I declare a missionPoint-pointer which I then pass into the function that will dynamically allocate it after parsing a file and figuring out how big it needs to be.
Currently this is how my code looks:

 missionPoint* mission;    //declaring the pointer
 parseMission(mission);

The parseMission function will then parse a specific file and find out how many missionPoints I need and will then allocate it in the following manner:

 mission = (missionPoint*) malloc(n * sizeof(missionPoint));

where n is the parsed number of missionPoints I need.

The problem is that within the function I can see the proper values but not outside of it; once the function returns it’s like nothing happened.

I would appreciate your help in making it so that the function modifies the original pointer and I can see the data from outside the function.

  • 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-05-23T05:19:57+00:00Added an answer on May 23, 2026 at 5:19 am

    You need to pass a reference to the pointer, i.e. a double pointer, because the address itself is going to be modified:

    missionPoint *mission;
    parseMission(&mission);
    

    The argument of parseMission should now be of type missionPoint ** instead of missionPoint *:

    *mission = (missionPoint*) malloc(n * sizeof(missionPoint));
    

    This wouldn’t be necessary if you only wanted to modify the memory mission is pointing to, but it cannot be avoided since you are assigning a new value to the pointer itself.

    Also note that casting the return value of malloc is not necessary in C.

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

Sidebar

Related Questions

I'm trying to create a function that would dynamically allocate an array, sets the
I'm trying to allocate and initialize an array inside a function, but I can't
I am trying to allocate a 2D dimension array of File Descriptors... So I
I'm currently trying to understand how to implement a 2-dimensional array of struct in
I am having trouble freeing a pointer in a pointer array (values). typedef struct
I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's
This question is in C++. I am trying to dynamically allocate an array of
I am trying to allocate a array of char*'s in C. I know the
I am trying to dynamically reallocate memory for an array of structs (actually an
I am trying to dynamically allocate an array of base(Student) classes, and then assign

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.