Introduction

In C programming language, a jagged array (also known as “ragged arrays” or “array of arrays”) is an array of arrays, where each element of the main array is a reference to another array, rather than a fixed-size element. This allows for the creation of arrays with a variable number of columns, where each row can have a different number of columns. Jagged arrays can be useful when working with irregular data sets, where the number of columns in each row is not fixed.

Jagged Array
Jagged Array

To understand it in detail, let us take an example of above picture. Here the array “array” has column size as variable.

There are many ways to create jagged array in C. Lets discuss some of them.

Jagged Array using pointer to an array

A jagged array can also be created using a pointer to an array. This method can be useful when working with irregular data sets, where the number of columns in each row is not fixed.

A jagged array can be declared as follows:

int (*jagged_array)[N];

This declares a pointer to an array of N integers.

Here is an example of how to create a jagged array with 3 rows and 4 columns in the first row, 2 columns in the second row, and 3 columns in the third row:

int array1[4] = {1, 2, 3, 4};
int array2[2] = {5, 6};
int array3[3] = {7, 8, 9};
int (*jagged_array)[4] = {array1, array2, array3};

Alternatively, you can also use malloc to create the jagged array:

int (*jagged_array)[4] = malloc(3 * sizeof(int[4]));
jagged_array[0] = (int[]){1, 2, 3, 4};
jagged_array[1] = (int[]){5, 6};
jagged_array[2] = (int[]){7, 8, 9};

In this way, jagged_array[0] points to array1, jagged_array[1] points to array2, jagged_array[2] points to array3, you can access the elements of the jagged array in the same way as a 2D array.

It is important to remember that when a jagged array is no longer needed, the memory allocated for the jagged array itself must be freed using the free() function.

Note: that this method has the benefit of not having to manage memory allocation of each row separately, also the size of each row is already known.

Jagged Array using Double pointer

Using double pointer, a jagged array can be declared as follows:

int **jagged_array;

This declares a pointer to a pointer of type int, which can be used to store a jagged array. The memory for the individual arrays that make up the jagged array must be allocated separately.

Here is an example of how to create a jagged array with 3 rows and 4 columns in the first row, 2 columns in the second row, and 3 columns in the third row:

int **jagged_array = (int**)malloc(3*sizeof(int*));
jagged_array[0] = (int*)malloc(4*sizeof(int));
jagged_array[1] = (int*)malloc(2*sizeof(int));
jagged_array[2] = (int*)malloc(3*sizeof(int));

Jagged Array using structure which contains an array

You can create a structure which contains an array and a variable to store the size of that array.

struct jagged_array{
    int* array;
    int size;
};

You can then create an array of this structure to create a jagged array.

struct jagged_array jagged_array[3];
jagged_array[0].array = (int[]){1, 2, 3, 4};
jagged_array[0].size = 4;
jagged_array[1].array = (int[]){5, 6};
jagged_array[1].size = 2;
jagged_array[2].array = (int[]){7, 8, 9};
jagged_array[2].size = 3;

In this way, jagged_array[0].array points to the first row, jagged_array[1].array points to the second row, jagged_array[2].array points to the third row, you can access the elements of the jagged array in the same way as a 2D array, and also you have the size of each row stored in the structure.

This approach is useful if you want to have a more flexible, and readable code, and also it’s easy to manage the memory allocation. You can also use malloc to allocate the memory for each row.

Jagged Array using Single dimensional array

A jagged array is to use a one-dimensional array and an array of indices but it makes the code somewhat complex and hard to maintain.

You can create an array of indices to store the starting position of each row in the one-dimensional array and an array to store the size of each row as shown below:

int one_dimensional_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int indices[3] = {0, 4, 6};
int sizes[3] = {4, 2, 4};

In this way, indices[0] stores the starting position of the first row, indices[1] stores the starting position of the second row, and so on. The sizes array stores the size of each row.

You can use these indices and sizes to access elements of the jagged array in the same way as a 2D array. This approach is useful when you are working with large data sets and want to save memory by using a single one-dimensional array.

Instead of making the static array, you can also allocate memory to “one_dimensional_array” at run time. In that way the code will become more dynamic and modular.

Conclusion

In conclusion, jagged arrays are an important concept in C programming language, which allows for the creation of arrays with a variable number of columns, where each row can have a different number of columns. They are useful when working with irregular data sets, where the number of columns in each row is not fixed. Jagged arrays can be implemented using a pointer to a pointer, a pointer to an array, a structure, or a one-dimensional array and an array of indices. Each of these methods has its own benefits and use cases. Dynamic arrays are also a related concept, which allows for changing the size of the array during runtime.

Reference

One thought on “Jagged Array: an array of arrays”

Comments are closed.