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

10 Sep 2023 Balmiki Mandal 0 C Programming

Creating an Array of Pointers & Allocating Memory with C

Arrays in C are a powerful data structure that can be used to store large amounts of data. They offer easy access to the elements they contain, making them ideal for manipulating data in many types of applications. But what if you need to dynamically allocate space for an array of pointers? How can you create an array of pointers and allocate memory for each element dynamically? This article will explain how to do just that using the C programming language.

Overview

In this article, we will use C to create an array of pointers and allocate memory for each element. We will then fill them with values and print the contents. Our code will look like this:

#include 

int main() 
{
    // Declare a pointer array
    int *array[10];

    // Allocate memory for each element
    for(int i = 0; i < 10; i++) 
    {
        array[i] = (int *)malloc(sizeof(int));  
    }

    // Fill the array with values
    for(int i = 0; i < 10; i++) 
    {
        *array[i] = i+1;
    }

    // Print the array
    for(int i = 0; i < 10; i++) 
    {
        printf("%d\n", *array[i]);
    }

    return 0;
}

Explanation

Let’s go through our code step-by-step. The first thing we do is declare an array of pointers:

int *array[10];

This statement creates an array that can hold pointers to 10 integers. The size of the array can be changed as needed.

Next, we use a for loop to allocate memory for each element of our array. We use the malloc() function, which allocates memory from the heap and returns a pointer to it. We cast the return value to a int * so that our pointer array holds pointers to integers:

for(int i = 0; i < 10; i++) 
{
    array[i] = (int *)malloc(sizeof(int));  
}

We then use another for loop to fill the array with values. We use the dereference operator (*) to access the value pointed to by array[i]:

for(int i = 0; i < 10; i++) 
{
    *array[i] = i+1;
}

Finally, we use one more for loop to print out the contents of the array. Again, we use the dereference operator (*) to access the value pointed to by array[i]:

for(int i = 0; i < 10; i++) 
{
    printf("%d\n", *array[i]);
}

And that's all there is to it! We now have an array of pointers that can store an arbitrary number of integers. The code above is a simple example, but it can be extended to handle more complex data structures or other types of data.

Conclusion

In this article, we learned how to create an array of pointers and allocate memory for each element dynamically in C. With a few lines of code, we created a powerful data structure that can be used to store and manipulate data in many types of programs. If you’re looking for an easy way to store data in your programs, an array of pointers may just be the answer you’re looking for.

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.