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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:16:23+00:00 2026-05-12T12:16:23+00:00

I have some code in a couple of different functions that looks something like

  • 0

I have some code in a couple of different functions that looks something like this:

void someFunction (int *data) {
  data = (int *) malloc (sizeof (data));
}

void useData (int *data) {
  printf ("%p", data);
}

int main () {
  int *data = NULL;

  someFunction (data);

  useData (data);

  return 0;
}

someFunction () and useData () are defined in separate modules (*.c files).

The problem is that, while malloc works fine, and the allocated memory is usable in someFunction, the same memory is not available once the function has returned.

An example run of the program can be seen here, with output showing the various memory addresses.

Can someone please explain to me what I am doing wrong here, and how I can get this code to work?


EDIT: So it seems like I need to use double pointers to do this – how would I go about doing the same thing when I actually need to use double pointers? So e.g. data is

int **data = NULL; //used for 2D array

Do I then need to use triple pointers in function calls?

  • 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-12T12:16:23+00:00Added an answer on May 12, 2026 at 12:16 pm

    You want to use a pointer-to-pointer:

    void someFunction (int **data) {
      *data = malloc (sizeof (int));
    }
    
    void useData (int *data) {
      printf ("%p", data);
    }
    
    int main () {
        int *data = NULL;
    
        someFunction (&data);
    
        useData (data);
    
        free(data);
    
        return 0;
    }
    

    Why? Well, you want to change your pointer data in the main function. In C, if you want to change something that’s passed in as a parameter (and have that change show up in the caller’s version), you have to pass in a pointer to whatever you want to change. In this case, that "something you want to change" is a pointer — so to be able to change that pointer, you have to use a pointer-to-pointer…

    Note that on top of your main problem, there was another bug in the code: sizeof(data) gives you the number of bytes required to store the pointer (4 bytes on a 32-bit OS or 8 bytes on a 64-bit OS), whereas you really want the number of bytes required to store what the pointer points to (an int, i.e. 4 bytes on most OSes). Because typically sizeof(int *)>=sizeof(int), this probably wouldn’t have caused a problem, but it’s something to be aware of. I’ve corrected this in the code above.

    Here are some useful questions on pointers-to-pointers:

    How do pointer to pointers work in C?

    Uses for multiple levels of pointer dereferences?

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

Sidebar

Related Questions

I have a couple of generic lists with completely different types of data. In
I was working on an abstract class to save on some code for a
We're considering using Python (IronPython, but I don't think that's relevant) to provide a
I'm having something of an issue with trying to load externally defined classes in
Long post.. sorry I've been reading up on this and tried back and forth
Last couple of months I've been wondering about all these password strength meters on
Just getting started on iPhone dev today and have run through Apple's HelloWorld tutorial:
I'm attempting to move a web app we have (written in Perl) from an
I am a complete noob to android but I have been programing c# for
I'm working on a java project with my team at work. To summarize, we

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.