In C, an array is a collection of elements of the same type stored in contiguous memory
locations. This organization allows efficient access to elements using their index. Arrays can
also be of different types depending upon the direction/dimension they can store the
elements. It can be 1D, 2D, 3D, and more. We generally use only one-dimensional, two-
dimensional, and three-dimensional arrays.
In this article, we will learn all about one-dimensional (1D) arrays in C, and see how to use
them in our C program.
One-Dimensional Arrays in C
A one-dimensional array can be viewed as a linear sequence of elements. We can only
increase or decrease its size in a single direction.
Only a single row exists in the one-dimensional array and every element within the array is
accessible by the index. In C, array indexing starts zero-indexing i.e. the first element is at
index 0, the second at index 1, and so on up to n-1 for an array of size n.
Syntax of One-Dimensional Array in C
The following code snippets shows the syntax of how to declare an one dimensional array
and how to initialize it in C.
1D Array Declaration Syntax
In declaration, we specify then name and the size of the 1d array.
elements_type array_name[array_size];
In this step, the compiler reserved the given amount of memory for the array but this step
does not define the value of the elements. They may contain some random values. So we
initialize the array to give its elements some initial valu
1D Array Initialization Syntax
In declaration, the compiler reserved the given amount of memory for the array but does
not define the value of the element. To assign values, we have to initialize an array.
elements_type array_name[array_size] = {value1, value2, ... };
This type of The values will be assigned sequentially, means that first element will contain
value1, second value2 and so on.
Note: The number of elements initialized should be equal to the size of the array. If more
elements are given in the initializer list, the compiler will show a warning and they will not be
considered.
This initialization only works when performed with declaration.
1D Array Element Accessing/Updating Syntax
After the declaration, we can use the index of the element along with the array name to
access it.
array_name [index]; // accessing the element
Then, we can also assign the new value to the element using assignment operator.
array_name [index] = new_value; // updating element
Note: Make sure the index lies within the array or else it might lead to segmentation fault.
To access all the elements of the array at once, we can use the loops as shown below.
Example of One Dimensional Array in C
The following example demonstrate how to create a 1d array in a c program.
// C program to illustrate how to create an array,
2D array
How to Initialize a 2D Array in C?
• List initializationa
• Initialization with zero
• Runtime initialization with zero
In C, a 2D Array is a type of multidimensional array in which data is stored in tabular form
(rows and columns). It has two dimensions so it can store the data and can expand in two
directions. In this article, we will learn how to initialize a 2D array in C.
There are three main ways to initialize the 2D array in C:
Table of Content
• List Initialization
• Initialization with Zero
• Runtime Initialization Using Loops
List Initialization
To initialize a 2D array, we can use a list of values enclosed inside the braces '{ }' and
separated by a comma. Each value in this list corresponds to an element in the array.
Syntax
int arr[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
The first 4 elements from the left will be filled in the first row, the next 4 elements in the
second row, and so on. We can also explicitly do this as:
int arr[3][4] = {{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}};
This method is called List Initialization of 2D Array.
C Program to Initialize 2D Array Using Initializer Lists
// C Program to illustrate how to initialize
// 2D array using initializer lists
#include <stdio.h>
int main() {
// Initializing a 3x4 2D array
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7},
{8, 9, 10, 11}};
// Printing the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
printf("\n");
return 0;
Output
0123
4567
8 9 10 11
Time Complexity: O(m * n), where m and n are the number of rows and columns.
Auxiliary Space: O(1)
Note: The number of elements in initializer list should always be either less than or equal to
the total number of elements in the array.
Initialization with Zero
We can initialize all the elements of a numeric, character or binary 2D array to the value 0
using the below syntax:
int arr[3][4] = {0}
C Program to Zero Initialize 2D Array
// C Program to illustrate how to initialize
// 2D array with Zero
#include <stdio.h>
int main() {
// Initializing a 3x4 2D array
int arr[3][4] = {0};
// Printing the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
printf("\n");
return 0;
Output
0000
0000
0000
Time Complexity: O(m * n), where m and n are the number of rows and columns.
Auxiliary Space: O(1)
Runtime Initialization Using Loops
Al the above methods were static initialization, i.e. element values are defined before the
compilation. When we want to initialize the 2D array elements one by one at runtime with
the values that have some sequential relation or taking input from any source, it is better to
use loops. We use two loops, one nested inside other. The outer loop is used to go from row
to row and the inner loop is used to initialize each element in that row.
C Program to Initialize 2D Array Using Loops
// C Program to illustrate how to initialize
// 2D array using loops
#include <stdio.h>
int main() {
// Define a 2D array
int arr[3][4];
// A variable that is assigned to the array elements
int count = 1;
// Initializing using loops
for (int i = 0; i < 3; i++) {
// Inner loop move through each element left to right
for (int j = 0; j < 4; j++) {
arr[i][j] = count++;
// Printing the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
return 0;
Output
1234
5678
9 10 11 12
Time Complexity: O(m * n), where m and n are the number of rows and columns.
Auxiliary Space: O(1)
We also can use this method to initialize an array from another array of same size by using
the values of the array elements instead of count variable.
// initialize it, update and access elements
#include <stdio.h>
int main()
// declaring and initializing array
int arr[5] = { 1, 2, 4, 8, 16 };
// printing it
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
printf("\n");
// updating elements
arr[3] = 9721;
// printing again
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
return 0;
Output
1 2 4 8 16
1 2 4 9721 16
Multidimensional Arrays in C - 2D and 3D Arrays
Last Updated : 25 Jul, 2025
A multi-dimensional array in C can be defined as an array that has more than one
dimension. Having more than one dimension means that it can grow in multiple directions.
Some popular multidimensional arrays include 2D arrays which grows in two dimensions,
and 3D arrays which grows in three dimensions.
// C program to demonstrate working of 2D arrays
#include <stdio.h>
int main() {
/*
Declare and initialize a 2×2 integer array.
arr[0][0] = 10, arr[0][1] = 20,
arr[1][0] = 30, arr[1][1] = 40
*/
int arr[2][2] = { {10, 20}, {30, 40} };
printf("2D Array Elements:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", arr[i][j]);
printf("\n");
return 0;
Syntax
The general form of declaring N-dimensional arrays is shown below:
type arrName[size1][size2]....[sizeN];
• type: Type of data to be stored in the array.
• arrName: Name assigned to the array.
• size1, size2,..., sizeN: Size of each dimension.
Examples:
// Two-dimensional array
int two_d[10][20];
// Three-dimensional array:
int three_d[10][20][30];
Size of Multidimensional Arrays
The total number of elements that can be stored in a multidimensional array can be
calculated by multiplying the size of both dimensions. Consider the array arr[10][20]:
• The array int arr[10][20] can store total of (10*20) = 200 elements.
To get the size in bytes, we multiply the size of a single element (in bytes) by the total
number of elements in the array.
• The size of array int arr[10][20] = 10 * 20 * 4 = 800 bytes, where the size of int is 4
bytes.
Types of Multidimensional Arrays
In C, there can be many types of arrays depending on their dimensions but two of them are
most commonly used:
1. 2D Array - Two Dimensional
2. 3D Array - Three Dimensional
2D Arrays in C
A two-dimensional array or 2D array is the simplest form of the multidimensional array. We
can visualize a two-dimensional array as one-dimensional arrays stacked vertically forming a
table with 'm' rows and 'n' columns. In C, arrays are 0-indexed, so the row number ranges
from 0 to (m-1) and the column number ranges from 0 to (n-1).
Declaration of 2D Array
A 2D array with m rows and n columns can be created as:
type arr_name[m][n];
For example, we can declare a two-dimensional integer array with name 'arr' with 10 rows
and 20 columns as:
int arr[10][20];
Initialization of 2D Arrays
We can initialize a 2D array by using a list of values enclosed inside '{ }' and separated by a
comma as shown in the example below:
int arr[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11};
or
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
The elements will be stored in the array from left to right and top to bottom. So, the first 4
elements from the left will be filled in the first row, the next 4 elements in the second row,
and so on. This is clearly shown in the second syntax where each set of inner braces
represents one row.
In list initialization, we can skip specifying the size of the row. The compiler will automatically
deduce it in this case. So, the below declaration is valid.
type arr_name[][n] = {...values...};
It is still compulsory to define the number of columns.
Note: The number of elements in initializer list should always be either less than or equal to
the total number of elements in the array.
Accessing Elements
An element in two-dimensional array is accessed using row indexes and column indexes
inside array subscript operator [].
arr_name[i][j]
2D Array Traversal
Traversal means accessing all the elements of the array one by one. We will use two loops,
outer loop to go over each row from top to bottom and the inner loop is used to access each
element in the current row from left to right.
for(int i = 0; i < row; i++){
for(int j = 0; J < col; j++){
arr[i][j];
The below example demonstrates the row-by-row traversal of a 2D array.
#include <stdio.h>
int main() {
// Create and initialize an array with 3 rows
// and 2 columns
int arr[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// Print each array element's value
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("arr[%d][%d]: %d ", i, j, arr[i][j]);
printf("\n");
return 0;
Output
arr[0][0]: 0 arr[0][1]: 1
arr[1][0]: 2 arr[1][1]: 3
arr[2][0]: 4 arr[2][1]: 5
How 2D Arrays are Stored in the Memory?
As an array, the elements of the 2D array have to be stored contiguously in memory. As the
computers have linear memory addresses, the 2-D arrays must be linearized so as to enable
their storage. There are two ways to achieve this:
• Row Major Order: This technique stores the 2D array row after row. It means that
the first row is stored first in the memory, then the second row of the array, then the
third row, and so on.
• Column Major Column: This technique stores the 2D array column after column. It
means that first column is stored first in the memory, then the second column, then
the third column, and so on.
This allows the random access to the 2D array elements as the computer need not keep
track of the addresses of all the elements of the array. It only tracks of the Base Address
(starting address of the very first element) and calculates the addresses of other array
elements using the base address.
Passing 2D Arrays to Functions
Passing 2D arrays to functions need a specialized syntax so that the function knows that the
data being passed is 2d array. The function signature that takes 2D array as argument is
shown below:
Dynamic Array in C
Last Updated : 23 Jul, 2025
Array in C is static in nature, so its size should be known at compile time and we can't change
the size of the array after its declaration. Due to this, we may encounter situations where
our array doesn't have enough space left for required elements or we allotted more than the
required memory leading to memory wastage. To solve this problem, dynamic arrays come
into the picture.
A Dynamic Array is allocated memory at runtime and its size can be changed later in the
program.
We can create a dynamic array in C by using the following methods:
1. Using malloc() Function
2. Using calloc() Function
3. Resizing Array Using realloc() Function
4. Using Variable Length Arrays(VLAs)
5. Using Flexible Array Members
1. Dynamic Array Using malloc() Function
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single
large block of memory with the specified size. It returns a pointer of type void which can be
cast into a pointer of any form. It is defined inside <stdlib.h> header file.
Syntax:
ptr = (cast-type*) malloc(byte-size);
We can use this function to create a dynamic array of any type by simply allocating a
memory block of some size and then typecasting the returned void pointer to the pointer of
the required type.
Example:
ptr = (int*) malloc(100 * sizeof(int));
In the above example, we have created a dynamic array of type int and size 100 elements.
Note: It is to be noted that if malloc fails to allocate the required memory, it returns the NULL
pointer. So, it is a good practice to check for NULL pointer to see if the memory is successfully
allocated or not.
Example:
// C program to create dynamic array using malloc() function
#include <stdio.h>
#include <stdlib.h>
int main()
// address of the block created hold by this pointer
int* ptr;
int size;
// Size of the array
printf("Enter size of elements:");
scanf("%d", &size);
// Memory allocates dynamically using malloc()
ptr = (int*)malloc(size * sizeof(int));
// Checking for memory allocation
if (ptr == NULL) {
printf("Memory not allocated.\n");
else {
// Memory allocated
printf("Memory successfully allocated using "
"malloc.\n");
// Get the elements of the array
for (int j = 0; j < size; ++j) {
ptr[j] = j + 1;
// Print the elements of the array
printf("The elements of the array are: ");
for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
}
}
return 0;
Output:
Enter size of elements:5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,
2. Dynamic Array Using calloc() Function
The “calloc” or “contiguous allocation” method in C is used to dynamically allocate the
specified number of blocks of memory of the specified type and initialized each block with a
default value of 0.
The process of creating a dynamic array using calloc() is similar to the malloc() method. The
difference is that calloc() takes arguments instead of one as compared to malloc(). Here, we
provide the size of each element and the number of elements required in the dynamic array.
Also, each element in the array is initialized to zero.
Syntax:
ptr = (cast-type*)calloc(n, element-size);
Example:
ptr = (int*) calloc(5, sizeof(float));
In the above example, we have created a dynamic array of type float having five elements.
Let's take another example to create a dynamic array using the calloc() method.
Example:
// C program to create dynamic array using calloc() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
// address of the block created hold by this pointer
int* ptr;
int size;
// Size of the array
printf("Enter size of elements:");
scanf("%d", &size);
// Memory allocates dynamically using calloc()
ptr = (int*)calloc(size, sizeof(int));
// Checking for memory allocation
if (ptr == NULL) {
printf("Memory not allocated.\n");
else {
// Memory allocated
printf("Memory successfully allocated using "
"malloc.\n");
// Get the elements of the array
for (int j = 0; j < size; ++j) {
ptr[j] = j + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
return 0;
Output:
Enter size of elements:6
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5, 6,
3. Dynamically Resizing Array Using realloc() Function
The “realloc” or “re-allocation” method in C is used to dynamically change the memory
allocation of a previously allocated memory.
Using this function we can create a new array or change the size of an already existing array.
Syntax:
ptr = realloc(ptr, newSize);
Let's take an example to understand this properly.
Example:
// C program to resize dynamic array using realloc()
// function
#include <stdio.h>
#include <stdlib.h>
int main()
{
// address of the block created hold by this pointer
int* ptr;
int size = 5;
// Memory allocates dynamically using calloc()
ptr = (int*)calloc(size, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
else {
printf("Memory successfully allocated using "
"calloc.\n");
// inserting elements
for (int j = 0; j < size; ++j) {
ptr[j] = j + 1;
printf("The elements of the array are: ");
for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
}
printf("\n");
size = 10;
int *temp = ptr;
// using realloc
ptr = realloc(ptr, size * sizeof(int));
if (!ptr) {
printf("Memory Re-allocation failed.");
ptr = temp;
else {
printf("Memory successfully re-allocated using "
"realloc.\n");
// inserting new elements
for (int j = 5; j < size; ++j) {
ptr[j] = j + 10;
printf("The new elements of the array are: ");
for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
return 0;
}
Output
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
Memory successfully re-allocated using realloc.
The new elements of the array are: 1, 2, 3, 4, 5, 15, 16, 17, 18, 19,