CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – How to create arrays dynamically?

We can create arrays in different ways in C. We already discussed creating non-dynamic arrays in C in our earlier article “C Programming – An introduction to Arrays“; I recommend you to read it before reading this article. Through this article, we are going to discuss how to create arrays dynamically in C.

Create arrays dynamically in C

Dynamically creating arrays means, we need to allocate the memory for arrays on the fly, depending on the requirements. Unlike static & fixed-size arrays where memory is not possible to release until program closure, we can release the memory for dynamically created arrays whenever needed. Especially this is really useful when we deal with large data.

To create arrays dynamically, we need to deal with the pointers. And we need to know about a few library functions in C those functions deals with dynamic memory allocation (using malloc, calloc & realloc functions).

The first thing we need to do, in order to create an array dynamically is, we need to declare a variable as a pointer type and allocate the memory to it. The below example will create a dynamic memory to store 10 integer elements;

int *ptr = (int *) malloc(10 * sizeof(int));

Next, we are going to learn how to access and modify the elements of an array. Note that, this is the same as for dynamic & non-dynamic arrays.

Accessing the elements of an array

We can access the elements from an array using either the index operator ([]) or using the pointer indirection operator (*). We always need to remember is, an array has boundaries, and we must have to use the index value which is within the boundary of an array to access its’ elements.

From the above example, ptr can hold 10 integer-type variables. So its’ boundary values are 0 and 9. To access the 4th element, we can write the code as ptr[3] or *(ptr + 3). Below is an example to iterate the array and display its values.

for (int index = 0; index < 10; index++)
            printf("%d %d\n", ptr[index], *(ptr + index));

Assigning values to the elements of an array

We need to remember to set the array values to 0 (zero) or the initial value when creating the dynamic arrays. We can use either calloc function or memset functions depend on the program’s needs. This is important because when the memory is created for an array its elements are filled with garbage values. We need to reset these before starting to use the arrays to avoid unexpected results.

The below example will fill all elements of the array ptr with the value 0 (zero).

memset(ptr, 0, 10 * sizeof(int));

Once the dynamically created arrays are initialized with the default values, we can start using them to assign the values using either the index operator ([]) or the pointer indirection operator (*), the same as accessing the array elements. The below example(s) will set the 4th element value of an array to 65.

ptr[3] = 65;

or

*(ptr + 3) = 65;

Free up the memory

Once we do not need the dynamically created array, we can release its memory using free library function.

free(ptr);

//Malin

C Programming – How to create arrays dynamically?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top