Dynamically Allocating Memory for an Array of Pointers – Example Code from Electro4u.net

10 Sep 2023 Balmiki Mandal 0 C Programming

How do you dynamically allocate memory for an array of pointers? Provide an example code snippet.

The process of dynamically allocating memory for an array of pointers requires the use of the malloc function. The malloc function takes in a single argument, which is the size of the memory block that needs to be allocated. In the case of an array of pointers, the number of pointers in the array must be specified, as well as the size of the data type that each pointer is pointing to.

Here is an example code snippet demonstrating how to use the malloc function to dynamically allocate memory for an array of 5 pointers to ints:

// Declare the pointer array 
int **ptr_array;

// Allocate memory to the pointer array 
ptr_array = malloc(5 * sizeof(int *)); 

// Allocate memory for each element in the array 
for (int i = 0; i < 5; i++) { 
    ptr_array[i] = malloc(sizeof(int)); 
}

// Deallocate memory from the pointer array 
for (int i = 0; i < 5; i++) { 
    free(ptr_array[i]); 
}

free(ptr_array);

In this example, we are first declaring a pointer array called ptr_array. We then use the malloc function to allocate memory for the array with a size of 5 pointers. We then use a loop to allocate memory for each individual element in the array, specifying the size of the data type that the pointer is pointing to. Finally, we use another loop to deallocate memory from each element in the array, followed by deallocating the array itself.

Example of how dynamically allocate memory for an array of pointers

C- Programming
#include
int main() {
  int n = 5;
  int *ptr;

  // Allocate memory for the array of pointers.
  ptr = malloc(sizeof(int *) * n);

  // Initialize the array of pointers to NULL.
  for (int i = 0; i < n; i++) {
    ptr[i] = NULL;
  }

  // Now, you can use the ptr array to store pointers to int variables.
  int a = 10;
  int b = 20;
  int *p1 = &a;
  int *p2 = &b;

  // Store the pointers to a and b in the array of pointers.
  ptr[0] = p1;
  ptr[1] = p2;

  // Print the values of the pointers in the array.
  for (int i = 0; i < n; i++) {
    printf("%d\n", *ptr[i]);
  }

  // Free the memory allocated for the array of pointers.
  free(ptr);

  return 0;
}

In this code, we first declare an integer variable n to store the size of the array. We then allocate memory for the array of pointers using the malloc() function. After the memory is allocated, we initialize the array of pointers to NULL.

Next, we create two integer variables a and b and a pointer variable p1 and p2. We then store the addresses of a and b in p1 and p2. Finally, we store the pointers p1 and p2 in the array of pointers.

At the end of the program, we free the memory allocated for the array of pointers using the free() function.

Further Reading:


What is Array of pointer? Give one example.

Array of Pointer in c programming language

How to Create An Array Of Pointers in C – A Step-by-Step Guide

What is the difference between Array of pointer and Pointer to Array?

array of pointers, string functions, data manipulation, efficient algorithms

Dynamically Allocating Memory for an Array of Pointers

Virtual Shorting Using an array of Pointer

Write a C function that takes an array of pointers to strings and sorts them in lexicographic (dictionary) order.

 Assignment of Array of Pointers in C

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.