Jagged Array in C++
An array in C++ is a group of identically typed elements that are kept in a single block of memory. On the other hand, the jagged array is a sort of array where each row's number of columns can vary. "Arrays of arrays" is another name for jagged arrays. We will look at the definition, usage, and examples of jagged arrays in C++ in this post.
Definition of Jagged Array
Each row of an array in a jagged array may contain a varied number of columns. Because it comprises numerous arrays, each with a different number of elements, the jagged array is sometimes known as an "array of arrays". The jagged array's elements are all arrays in and of itself.
For example, consider the following jagged array:
In the last example, we generated a jagged array made up of three separate arrays, the first of which contained two elements, the second three, and the third four.
The use of a jagged array
A jagged array can be created in C++ by utilizing a two-dimensional array of pointers. The array's elements are all pointers to arrays of numbers. Using the new keyword, we can dynamically create memory for each row of the jagged array.
The following C++ code demonstrates how to build a jagged array:
In the example above, we used a two-dimensional array of pointers to construct a jagged array. Using the new keyword, we allocated memory for each row of the jagged array. The square bracket notation has also been used to access the jagged array's components.
Using the delete keyword, we have finally dealtlocated the memory set aside for the jagged array. To prevent memory leaks, it is crucial to deallocate the memory allocated for the jagged array.
Examples of Jagged Array
Let's consider some examples of jagged arrays to understand them better.
Different length arrays are contained in a jagged array:
1 2 3 4 5 6 7 8 9
This result demonstrates that a jagged array in C++ was successfully generated and printed.
In this example, we've built a jagged array with three arrays, the first of which has two elements, the second of which has three elements, and the third of which has four elements. For each row of the jagged array, memory has been allocated using the new keyword.
After that, the elements of the jagged array were printed using a nested for loop. The inner loop iterates through the columns of each row, while the outer loop iterates over the rows of the jagged array. In order to limit the output of the inner loop to the number of elements in each row, we utilized i+2 as the halting condition. In order to prevent memory leaks, we have finally dealtlocated the RAM allotted for each row using the delete operator.
Different data types are contained in a jagged array:
intArray = 1 intArray = 2 charArray = a charArray = b floatArray = 1.1 floatArray = 2.2
In the above example, we've made a jagged array that contains arrays of various data kinds. The arrays are kept in the jagged array via a void* pointer. Each array has had memory allocated for it, and the appropriate member of the jagged array has been given access to each array's address.
By casting the void* pointer to the proper data type, we can gain access to the jagged array's components. For instance, using the square bracket syntax and casting the void* pointer to an int* pointer, we may access the first element of the intArray. In order to demonstrate that the program ran well, we have finally returned 0.
In this post, we looked at the definition, usage, and examples of C++'s jagged arrays. Each row of an array in a jagged array may contain a varied number of columns. A two-dimensional array can be used to implement a jagged array.