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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:21:38+00:00 2026-05-14T05:21:38+00:00

I have to allocate a struct from within another function, obviously using pointers. I’ve

  • 0

I have to allocate a struct from within another function, obviously using pointers.
I’ve been staring at this problem for hours and tried in a million different ways to solve it.

This is some sample code (very simplified):

...
some_struct s;
printf("Before: %d\n", &s);
allocate(&s);
printf("After: %d\n", &s);
...

/* The allocation function */
int allocate(some_struct *arg) {

arg = malloc(sizeof(some_struct));
printf("In function: %d\n", &arg);

return 0;
}

This does give me the same address before and after the allocate-call:

Before: -1079752900
In function: -1079752928
After: -1079752900

I know it’s probably because it makes a copy in the function, but I don’t know how to actually work on the pointer I gave as argument. I tried defining some_struct *s instead of some_struct s, but no luck. I tried with:

int allocate(some_struct **arg)

which works just fine (the allocate-function needs to be changed as well), BUT according to the assignment I may NOT change the declaration, and it HAS to be *arg.. And it would be most correct if I just have to declare some_struct s.. Not some_struct *s.
The purpose of the allocation function is to initialize a struct (a some_struct), which also includes allocating it.

One more thing I forgot to mention. The return 0 in the allocate function is reserved for some status messages and therefore I can’t return the address using this.

  • 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-14T05:21:39+00:00Added an answer on May 14, 2026 at 5:21 am

    I highly doubt this is what your teacher had in mind, but you can cheat using a series of legal type conversions.

       int allocate(some_struct *arg) 
       /* we're actually going to pass in a some_struct ** instead. 
          Our caller knows this, and allocate knows this.  */
       { 
          void *intermediate = arg;  /* strip away type information */
          some_struct **real_deal = intermediate;  /* the real type */
          *real_deal = malloc(sizeof *real_deal); /* store malloc's return in the 
                                                     object pointed to by real_deal */
          return *real_deal != 0;  /* return something more useful than always 0 */
       }
    

    Then your caller does the same:

       {
          some_struct *s; 
          void *address_of_s = &s; 
          int success = allocate(address_of_s); 
          /* what malloc returned should now be what s points to */
          /* check whether success is non-zero before trying to use it */
       }
    

    This relies on a rule in C that says any pointer to an object can be implicitly converted to a void pointer, and vice-versa, without loss.

    Note that formally this is undefined, but it is all but sure to work. While any object pointer value is required to be able to convert to a void* and back without loss, there is nothing in the language that guarantees that a some_struct* can store a some_struct** without loss. But it has a very high likelihood of working just fine.

    Your teacher gave you no option but to write formally illegal code. I don’t see that you have any other option besides “cheating” like this.

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

Sidebar

Related Questions

My problem is the following - I have this lines of code: // allocate
I have the following the functions: This function will get every IP Addresses from
I have a class constructor like this: public JavoImageCorrectedDataHeader() { ByteBuffer buffer = ByteBuffer.allocate(this.size());
I have some code intended to get a struct from a byte array: public
I have a problem with an application I'm currently developing. In this program I
I am working on network programming using epoll and I have this code... int
I have been given a DLL (InfoLookup.dll) that internally allocates structures and returns pointers
I am using C++ on Windows 7 with MSVC 9.0, and have also been
I have been working on reading in an audio asset using AVAssetReader so that
Working on a problem where I have to read data from a file into

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.