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 593417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:50:37+00:00 2026-05-13T15:50:37+00:00

I need to create a structure inside a function (dynamically with malloc) Then i

  • 0

I need to create a structure inside a function (dynamically with malloc)
Then i need to be able to send it to my main, and use it there.
I have no problems creating it, I simply need help sending it to my main, and I’m also unsure of how to access it once I get it there.

struct retValue * fn() {
struct retValue
{
    int number;
};

struct retValue* st = malloc(sizeof(*st));


return(???);   
}

That is the code i have so far.

Thanks for any help. Let me know if you need something clarified.

EDIT:

Ok Some clarification is needed.

What I am trying to achieve, is the ability to pass a structure through a function to my main. Inside the function i must declare variables, and assign them values. Then in the main I must print each variable of the structure to the screen. No global variables can be used (and thus i assume no global structures).

Hopefully that clarifies things.

EDIT 2:

I’ve figured out my problem. For those interested, I needed to have the structure prototype outside of my functions first. Then i could pass st and then access it properly.
Thanks to all, and sorry for the poor wording.

  • 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-13T15:50:37+00:00Added an answer on May 13, 2026 at 3:50 pm
    // make the structure def global so that main() and fun() know about it.
    struct retValue {
     int number;
    };    
    
    // function fn returns a pointer to the structure retValue
    struct retValue * fn() {     
    
     struct retValue* st = malloc(sizeof(*st));
    
     // return the pointer to the newly allocated structure.
     return(st);   
    }
    
    int main() {
    
     // call fn() to create a new structure dynamically.
     struct retValue *st = fn();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data and need to create a json file with this structure
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I have a folder structure at server side. Inside the folders there are files
I need to create a structure that holds a variable number of 'char[2]'s, i.e.
Ok, so here's the entire structure I'm trying to create. I need to create
I use MS SQL 2008 R2, I need create a Table with a CHECK
I have a situation where I need to use some strings temporarily but I've
i'm creating a django application inside a virtualenv. I have a directory structure like
I need to create a table with this structure: Column A Column B Column
I need create custom dialog and put JPanel into it. Is it possible?

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.