Example
array[][]={{1,2,3},{4,56,7},{3,7,0},{4,7,9,6,0}}
The subsequent methods for executing the jagged array include:
1. Using an array and a pointer (Static Jagged Array)
- First, declare 1-D arrays with the total number of rows we'll need, and then use a pointer to access the array.
- The number of columns (or components) in the row will equal the size of each array (array for the items in the row).
- After that, define a 1-D array of pointers to contain the row addresses.
- The total number of rows in the jagged array is equal to the size of the 1-D array.
Example:
Let's consider a scenario to demonstrate the jagged array implementation using arrays and pointers in the C programming language.
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row0s[4] = { 1, 5, 3, 8 };
int row1s[2] = { 5, 9 };
int* jaggedArray[2] = { row0s, row1s };
// Array for storing the size
int Sizes[2] = { 4, 2 }, ke = 0;
// loop for displaying the elements
for (int i = 0; i < 2; i++) {
// address is stored in the pointer
int* ptrs = jaggedArray[i];
for (int j = 0; j < Sizes[ke]; j++) {
printf("%d ", *ptrs);
//The pointer will move to the next element of the row
ptrs++;
}
printf("\n");
ke++;
//The pointer will move to the next row
jaggedArray[i]++;
}
return 0;
}
Output:
Output
1 5 3 8
5 9
2. By using the Pointer array
- Here, declare a pointer array (jagged array), the size of which will be the total number of rows necessary in the Jagged array.
- After that, allocate memory for the total amount of elements that are desired in this row for each reference in the array.
Example:
Let's consider an instance to demonstrate the jagged array using a pointer array in the C programming language.
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
//declaration of the number of rows
int* jaggedArray[2];
// memory allocation at 0 row
jaggedArray[0] = malloc(sizeof(int) * 1);
// memory allocation at 1 row
jaggedArray[1] = malloc(sizeof(int) * 3);
// an array which is used for storing the size
int Sizes[2] = { 1, 3 }, ke = 0, numbers = 500;
// input array
for (int i = 0; i < 2; i++) {
int* pt = jaggedArray[i];
for (int j = 0; j < Sizes[ke]; j++) {
*pt = numbers++;
//The pointer moves over the array
pt++;
}
ke++;
}
ke = 0;
// display the elements of the array
for (int i = 0; i < 2; i++) {
int* pt = jaggedArray[i];
for (int j = 0; j < Sizes[ke]; j++) {
printf("%d ", *pt);
// pointer moves to the next element
pt++;
}
printf("\n");
ke++;
// pointer moves to the next row
jaggedArray[i]++;
}
return 0;
}
Output:
Output
500
501 502 503
Advantages of Jagged Array or Array of Arrays in C:
Several advantages of Jagged array or Array of Arrays are as follows:
- Memory Efficiency: Jagged Array allows for the storage of arrays of variable lengths, decreasing memory use by allocating space only for the needed components.
- Flexibility: Jagged Array allows for differently shaped data structures with variable sub-array sizes.
- Dynamic nature: Jagged Array allows for dynamic scaling of specific arrays inside the structure without impacting others.
- Cache Locality: Jagged Array provides greater cache usage due to contiguous memory allocation, which could result in quicker access times as compared to jagged arrays.
- Regular Data Structures' Simplicity: Jagged Arrays are ideal for regular grid-style data with the same amount of columns in each row.
- Ease of use: Multidimensional arrays with identical dimensions are easier to view and operate.