Table of contents
1.
Single Dimensional Array
1.1.
Declaring Single-Dimensional Array 
1.2.
Declaration with Fixed Size  
1.2.1.
Declaration with Initializer
1.2.2.
Dynamic Declaration in C
1.2.3.
Example
1.3.
C
2.
Multi Dimensional Array
2.1.
Two Dimensional Array
2.1.1.
Declaration with Fixed Size  
2.1.2.
Declaration with Initializer
2.1.3.
Dynamic Declaration in C
2.1.4.
Example
2.2.
C
2.3.
Three Dimensional Array
2.3.1.
Example
2.4.
C
3.
Properties of Arrays in C
3.1.
Fixed Size
3.2.
Contiguous Memory 
3.3.
Zero Based Indexing
3.4.
Homogeneous Elements
3.5.
Direct Memory Access
4.
Frequently Asked Questions
4.1.
What is the syntax of array?
4.2.
What are applications of array in C?
4.3.
What are the types of pointers?
5.
Conclusion
Last Updated: Jul 17, 2024
Easy

Types of Array in C

Author Rishabh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Types of array in C

Arrays in C are the collections of variables of the same data type stored in a contiguous memory location. They allow us to store large amounts of data efficiently in a structured way. They are accessed using an index. 

array in C

Arrays in C are classified into two types i.e single-dimensional Array and multi-dimensional arrays. A single-dimensional array is a collection of values of the same data type, whereas a two-dimensional array represents a mathematical matrix. We will look at both types in detail.

Single Dimensional Array

single-dimensional array is a collection of values of the same data type. They are stored in contiguous memory locations and can be accessed using an index. We will look at the example of a single-dimensional array.

NOTE: Single Dimensional array is used to store and manipulate lists or sequences of data. They can also be used in implementation of data structures such as stacks, queues and linked lists.

Declaring Single-Dimensional Array 

There are multiple ways in which we can declare a single dimensional array. Some of them are:

Declaration with Fixed Size  

The simplest way to declare the array is to specify its size at compile time. First, let’s look at the syntax for declaring an array with fixed size.

data_type array_name[array_size];
You can also try this code with Online C Compiler
Run Code
  • where ‘data_type’ is the type of the data, such as int, float.
  • ‘array_name’ is the name of the array you wish to give.
  • ‘array_size’ is the size of the array.

For example, to declare an array of 10 integers we would write:

int arr[10];
You can also try this code with Online C Compiler
Run Code

Declaration with Initializer

We can also initialize an array at the time of declaration using an initializer list. 

The syntax for an declaring an array with initializer is:

data_type array_name[] = {val1, val2, …, valN};
You can also try this code with Online C Compiler
Run Code
  • where ‘data_type’ is the type of the data such as int, float.
  • ‘array_name’ is the name of the array you wish to give.
  • ‘val1’,..., ‘valN’ are the values to be given.

Now let’s look at an example of an array declared with initializer:

int arr[] = {1, 2, 3, 4, 5};
You can also try this code with Online C Compiler
Run Code

Dynamic Declaration in C

We can use malloc() at runtime to dynamically allocate memory for an array. The malloc() function is commonly used for dynamic allocation in C, and it returns a pointer to a block of memory of the specified size. It is important to remember to free dynamically allocated memory using the free() function when it is no longer needed, to avoid memory leaks. 

Let’s look at the syntax for dynamic declaration in C:

data_type *array_name = (data_type*)malloc(array_size * sizeof(data_type));
You can also try this code with Online C Compiler
Run Code
  • where ‘data_type’ is the type of the data such as int, float.
  • ‘array_name’ is the name of the array you wish to give.
  • ‘(data_type*)malloc(array_size * sizeof(data_type))’ this is the dynamic allocation statement, malloc is used to allocate a block of memory of specified size in bytes. sizeof is used to calculate the size required for each element based on the data type.

For example to declare an array of 10 integers dynamically. We would write:

int *arr = (int*)malloc(10 * sizeof(int));
You can also try this code with Online C Compiler
Run Code

Example

Now we will do some operations on an array. Let's learn to modify the 2nd element of the array.

Later we will try to print all the elements of the array.

  • C

C

#include <stdio.h>

int main() {
// Initialize an array of integers
int arr[5] = {1, 2, 3, 4, 5};

printf("Array before modification\n");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Access an element in the array using its index
int first = arr[1];
printf("The value of 2nd element of the array before modification is %d\n", first);

// Modify an element in the array using its index
arr[1] = 10;
printf("The value of 2nd element of the array after modification is %d\n", arr[1]);

printf("Array after modification\n");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

single

Multi Dimensional Array

multi-dimensional array is a collection of values of the same data type. Multi Dimensional array is used in analysing and processing data with multiple dimensions, such as performing simulations on a grid.

Two Dimensional Array

2-D arrays are the most used in multi-dimensional arrays. These are generally stored in the form of rows and columns and are used to represent matrices. 

Let's see how to declare 2-dimensional array in C.

Declaration with Fixed Size  

The simplest way to declare the array is to specify its size at compile time. 

Let’s look at the syntax for declaring a multidimensional array with fixed size:

data_type array_name[rows][columns];
You can also try this code with Online C Compiler
Run Code
  • where ‘data_type’ is the type of the data such as int, float.
  • ‘array_name’ is the name of the array you wish to give.
  • and rows and columns are the sizes of rows and columns.

For example to declare a 2D array with 4 rows and 5 columns we use:

int arr[4][5];
You can also try this code with Online C Compiler
Run Code


Declaration with Initializer

We can also initialize an array at the time of declaration using an initializer list. 

The syntax for an declaring a multidimensional array with initializer is:

data_type array_name[size1][size2] = {{value11, value12, ..., value1N}, {value21, value22, ..., value2N}, ..., {valueM1, valueM2, ..., valueMN}};
You can also try this code with Online C Compiler
Run Code
  • where ‘data_type’ is the type of the data such as int, float.
  • ‘array_name’ is the name of the array you wish to give.
  • ‘size1’ and ‘size2’ are the sizes of the rows and columns respectively.
  • ‘value11’, …,'value1N' are the values of the first row of the array.
  • Similarly ‘valueM1’, …,'valueMN' are the values of the Mth row of the array.

For example to declare a 2D array with 2 rows and 3 columns and initialize it with values.

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
You can also try this code with Online C Compiler
Run Code

Dynamic Declaration in C

As we have done for single Dimensional array. We can use malloc() at runtime to dynamically allocate memory for a multi-dimensional array. 

Let’s look at the syntax for dynamic declaration in C:

data_type **array_name = (data_type**)malloc(rows * sizeof(data_type*));
for (int i = 0; i < rows; i++) {
    array_name[i] = (data_type*)malloc(columns * sizeof(data_type));
}
You can also try this code with Online C Compiler
Run Code
  • where ‘data_type’ is the type of the data such as int, float.
  • ‘array_name’ is the name of the array you wish to give.
  • ‘(data_type**)malloc(rows * sizeof(data_type*))’ this is the dynamic allocation statement, malloc is used to allocate a block of memory of specified size in bytes. sizeof is used to calculate the size required for each element based on the data type.
  • The ‘for’ loop is used to declare the elements in the 2D array.

For example to declare a 2D array with 2 rows and 3 columns dynamically we use

int **arr = (int**)malloc(2 * sizeof(int*));
for (int i = 0; i < 2; i++) {
    arr[i] = (int*)malloc(3 * sizeof(int));
}
You can also try this code with Online C Compiler
Run Code

Example

Now let’s look at an example of a 2-dimensional array and perform some operations on it. 

  • We will initialise a two-dimensional matrix of size 3*3. 
  • First, we will try to access the element matrix[0][0], which is 10. 
  • Next, we will change the element in the second row and the third column using matrix[1][2] to 100. 
  • Later we will print the matrix elements using two for loops.
     
  • C

C

#include <stdio.h>

int main() {
/*
Declare and initialize a
two-dimensional array of integers
*/
int matrix[3][3] = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}};

printf("Matrix before mdification\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
/*
Access an element in the matrix
using its row and column indices
*/
int first = matrix[0][0];
printf("The first element in the matrix is %d\n", first);

/*
Modify an element in the matrix
using its row and column indices
*/
matrix[1][2] = 100;
printf("The element in row 2, column 3 is changed to %d\n", matrix[1][2]);

printf("Matrix after mdification\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

2 Dimensional array

Three Dimensional Array

Three dimensional arrays are special forms of multidimensional arrays which are used to represent three dimensional coordinates in programming language. It is an extension of the concept of two dimensional array where each element is accessed using three dimensions instead of two. 

The syntax for a three dimensional array is:

dataType arrayName[size1][size2][size3];
You can also try this code with Online C Compiler
Run Code


Here, data type represents the type of data such as ‘int’ or ‘float’. ‘size1’, ‘size2’ and ‘size3’ represent the sizes of the array in three dimensions.

Example

Now we will look at an example of a three-dimensional array. We will initialize a three-dimensional array and print that array using three for loops.

  • C

C

#include <stdio.h>

int main() {
// Initializing a three-dimensional array
 int myArray[2][2][2] = {
 {{1, 2}, {5, 6}},
 {{13, 14}, {17, 18}}
 };

// Printing the array
 printf("Printing the Array\n");

 for (int i = 0; i < 2; i++) {
   for (int j = 0; j < 2; j++) {
     for (int k = 0; k < 2; k++)
       printf("%d ", myArray[i][j][k]);
     printf("\n");
   }
 }
 return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output 

multidimensional array


Also see, Short int in C Programming

Properties of Arrays in C

Fixed Size

Arrays in C have a fixed size. It means once you define the size of the array, then it cannot be changed during runtime.

Contiguous Memory 

The elements of an array in C are stored in contiguous memory locations which allows for efficient access to array elements using index notations.

Zero Based Indexing

Array Indexing in C starts from 0. The first element starts with index 0 and the second element with 1 and so on.

Homogeneous Elements

Arrays in C store data of the same type. All elements must have the same data type which is determined at the time of declaration.

Direct Memory Access

Elements can be accessed directly from their indexes, allowing for efficient and random access to array elements.

Frequently Asked Questions

What is the syntax of array?

The syntax for declaring an array in C is:

datatype arrayName[arraySize];

What are applications of array in C?

Arrays in C are used for various applications, such as storing multiple values of the same type, implementing data structures like matrices and vectors, creating buffers for I/O operations, and managing collections of data for sorting and searching algorithms.

What are the types of pointers?

Types of pointers in C include:

  1. Null Pointers: Pointers that do not point to any memory location.
  2. Void Pointers: Pointers with no specific data type.
  3. Function Pointers: Pointers that point to functions.
  4. Pointers to Pointers: Pointers that point to another pointer.

Conclusion

In conclusion, arrays in C come in two main types: single-dimensional arrays for linear collections and multi-dimensional arrays for matrices. Character and string arrays are also common. Arrays provide a fundamental data structure for storing and manipulating elements of the same data type.

And many more on our platform Code360

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!
However, you may consider our paid courses to give your career an edge over others!

Live masterclass