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

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't use statements in the middle of your class.… May 16, 2026 at 5:48 pm
  • Editorial Team
    Editorial Team added an answer The easiest way would be to just declare it as… May 16, 2026 at 5:48 pm
  • Editorial Team
    Editorial Team added an answer If you use an auto-increment field, you can easily write… May 16, 2026 at 5:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hi Im trying to create the following structure in div's, but I just need
I have a User control (because I use the same in other page, so
While developing my ASP.NET MVC, I have started to see the need for a
I need to have a list(array, ect) of objects that have a static values.
I have a special list (a sort of queue, as in the data structure,
I'm trying to create a JS-script to make modifications to add a footer to
I have a navigation menu that has submenus slide down on hover. The menu
I have a string buffer: b = create_string_buffer(numb) where numb is a number of
I have the table Tb ID | Name | Desc ------------------------- 1 | Sample
I am having an incredibly difficult time to decorate a Zend form the way

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.