# Jagged Array or Array of Arrays in C with Examples

**Prerequisite:** Arrays in C

**Jagged array** is **array of arrays** such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays.

**Example:**

arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}, {5} };

Below are the methods to implement the jagged array in C:

**Using array and a pointer (Static Jagged Array)**- First declare 1-D arrays with the number of rows you will need,
- The size of each array (array for the elements in the row) will be the number of columns (or elements) in the row,
- Then declare a 1-D array of pointers that will hold the addresses of the rows,
- The size of the 1-D array is the number of rows you want in the jagged array.

Below is the implementation of the above approach:

**Example:**`// C program to show the`

`// implementation of Jagged Arrays`

`#include <stdio.h>`

`#include <stdlib.h>`

`int`

`main()`

`{`

`int`

`row0[4] = { 1, 2, 3, 4 };`

`int`

`row1[2] = { 5, 6 };`

`int`

`* jagged[2] = { row0, row1 };`

`// Array to hold the size of each row`

`int`

`Size[2] = { 4, 2 }, k = 0;`

`// To display elements of Jagged array`

`for`

`(`

`int`

`i = 0; i < 2; i++) {`

`// pointer to hold the address of the row`

`int`

`* ptr = jagged[i];`

`for`

`(`

`int`

`j = 0; j < Size[k]; j++) {`

`printf`

`(`

`"%d "`

`, *ptr);`

`// move the pointer to the`

`// next element in the row`

`ptr++;`

`}`

`printf`

`(`

`"\n"`

`);`

`k++;`

`// move the pointer to the next row`

`jagged[i]++;`

`}`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**1 2 3 4 5 6

**Using an array of pointer (Dynamic Jagged Array)**- Declare an array of pointers (jagged array),
- The size of this array will be the number of rows required in the Jagged array
- Then for each pointer in the array allocate memory for the number of elements you want in this row.

Below is the implementation of the above approach:

**Example:**`// C program to show the`

`// implementation of Jagged Arrays`

`#include <stdio.h>`

`#include <stdlib.h>`

`int`

`main()`

`{`

`// 2 Rows`

`int`

`* jagged[2];`

`// Allocate memory for elements in row 0`

`jagged[0] =`

`malloc`

`(`

`sizeof`

`(`

`int`

`) * 1);`

`// Allocate memory for elements in row 1`

`jagged[1] =`

`malloc`

`(`

`sizeof`

`(`

`int`

`) * 3);`

`// Array to hold the size of each row`

`int`

`Size[2] = { 1, 3 }, k = 0, number = 100;`

`// User enters the numbers`

`for`

`(`

`int`

`i = 0; i < 2; i++) {`

`int`

`* p = jagged[i];`

`for`

`(`

`int`

`j = 0; j < Size[k]; j++) {`

`*p = number++;`

`// move the pointer`

`p++;`

`}`

`k++;`

`}`

`k = 0;`

`// Display elements in Jagged array`

`for`

`(`

`int`

`i = 0; i < 2; i++) {`

`int`

`* p = jagged[i];`

`for`

`(`

`int`

`j = 0; j < Size[k]; j++) {`

`printf`

`(`

`"%d "`

`, *p);`

`// move the pointer to the next element`

`p++;`

`}`

`printf`

`(`

`"\n"`

`);`

`k++;`

`// move the pointer to the next row`

`jagged[i]++;`

`}`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**100 101 102 103

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Digits of element wise sum of two arrays into a new array
- Divide array into two sub-arrays such that their averages are equal
- Maximum array from two given arrays keeping order same
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Combinations from n arrays picking one element from each array
- Merging elements of two different arrays alternatively in third array
- Maximize array sum by concatenating corresponding elements of given two arrays
- Find original array from encrypted array (An array of sums of other elements)
- Number of ways to divide an array into K equal sum sub-arrays
- Maximise array sum after taking non-overlapping sub-arrays of length K
- Break an array into maximum number of sub-arrays such that their averages are same
- Count arrays of length K whose product of elements is same as that of given array
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Find the sums for which an array can be divided into sub-arrays of equal sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.