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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:01:54+00:00 2026-05-27T10:01:54+00:00

Here is how I malloc an int var and then access this var outside

  • 0

Here is how I malloc an int var and then access this var outside of the function

int f1(int ** b) {
  *b = malloc(sizeof(int)); 
  **b = 5;
}

int main() {
  int * a;
  f1(&a);
  printf("%d\n", *a);
  // keep it clean : 
  free(a);
  return 0;
}

Using same logic above, how do I malloc a 1 dim array inside a function and then access it outside of the func?

Please help, I am bit confused with pointers to array.

  • 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-27T10:01:55+00:00Added an answer on May 27, 2026 at 10:01 am

    In exactly the same way but with some different arithmetic. You can think of what you are doing now as allocating an array with one element. Just multiply sizeof(int) by the number of elements you want your array to have:

    int f1(int ** b, int arrsize) {
      *b = malloc(sizeof(int) * arrsize);
    
      // then to assign items:
      (*b)[0] = 0;
      (*b)[1] = 1;
      // etc, or you can do it in a loop
    }
    
    int main() {
      int * a, i;
      int size = 20;
    
      f1(&a, size); // array of 20 ints
    
      for (i = 0; i < size; ++i)
          printf("%d\n", a[i]); // a[i] is the same as *(a + i)
                                // so a[0] is the same as *a
    
      // keep it clean : 
      free(a);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a little snippet of code from Wikipedia's article on malloc(): int *ptr;
I'm trying to write a size function like this: size(void *p,int size); Which would
Here is my code #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char
Here's a coding problem for those that like this kind of thing. Let's see
I saw this bit of code in another thread void foo { int *n
I know this question may be marked as a duplicate of difference between malloc
Here I have a pointer to the first element and int to hold the
Here is the code: .... typedef struct { int buf[10]; long head, tail; int
My question here is I had seen code like this for a multithreading application:
I'm trying to use malloc() and sizeof() to create a struct on the heap.

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.