Arrays in C – Properties, Syntax and Examples

Arrays are one of the most conceptual and used features of the C programming language. It is the simplest data structure algorithm where random elements can be accessed with their index number. It is one of the simplest programming languages. And, you can easily use data structure algorithms in C.

In C, arrays are structured data types. Let us learn more about it.

What are Arrays in C?

Arrays are simply a collection of similar data types stored at contiguous memory locations. It can store primitive types of data like int, char, float, double etc. With the help of the arrays, a programmer can access the elements very easily.

Suppose, you want to store marks of 20 students then you might try to declare 20 variables like student1_marks, student2_marks etc. But what if i tell you that you can do all those with a single variable named array. By the help of a few lines of code, you can access those elements.

     0             1         2           3        4

Array of size 5

Properties of array in C

  • An array is a variable that can store a fixed-size collection of elements of the same data type.
  • You can access the elements inside of an array randomly. You can also calculate the address of each element in an array.
  • Elements of the array stored at contiguous memory locations.

Advantages of an array in C

  • You can access the elements of the array randomly.
  • You can also sort the elements in an array with just a few lines of code.
  • It is easy to access the data of the array.

Disadvantages of an array in C

  • The array has to be fixed size. You can not exceed the limit of it. And it does not have the feature to grow dynamically like LinkedList.

How to declare an array?

Syntax for declaring an array:-

dataType array_name[arraySize];

Example:- Declare an array

int student_marks[20];
char student_name[10];
float numbers[5];

Note:- After the declaration, size of the array cannot be changed.

Access elements of an array in C

In C, you can access the elements of the array using their indices.

Suppose, you declare an array like below:-

int numbers[5];

If you want to access the first element of the above array then do numbers[0] and if you want to access the second element then do numbers[1] and so on.

  • The first index of the array is 0 not 1. That’s why numbers[0] is the first element.
  • To access the last element of the array, do numbers[4].
numbers[0] numbers[1] numbers[2] numbers[3] numbers[4]

2                             3                            4                     5                           6

Initialize an array:-

At the time of declaration, it is possible to initialize an array.

Syntax to initialize an array:-

int numbers[5] = {2, 3, 4, 5, 6};

You can also initialize an array like below:-

int numbers[] = {2, 3, 4, 5, 6};

In case, if you don’t specify the size of the array during initialization then the compiler will know it automatically.

numbers[0] = 2
numbers[1] = 3
numbers[2] = 4
numbers[3] = 5
numbers[4] = 6

Basic Example of an array:-

#include<stdio.h>

int main()
{
int numbers[5] = {2, 3, 4, 5, 6};
printf("TechVidvan Tutorial: Basic Example of an array!");
printf("First number is : %d\n",numbers[0]);
printf("Third number is : %d\n",numbers[2]);
printf("Last number is : %d\n",numbers[4]);
}

Output

TechVidvan Tutorial: Basic Example of an array!
First number is : 2
Third number is : 4
Last number is : 6

Change the value of array elements:-

In C, you can also change the elements of the array after declaring it.

Example:- Changing the value of array elements

#include<stdio.h>

int main()
{
int numbers[5] = {2, 3, 4, 5, 6};
printf("TechVidvan Tutorials: Change value of array elements!");
numbers[0] = 4; // changing value from 2 to 4!
numbers[2] = 5; // changing value from 4 to 5!
numbers[4] = 10; // changing value from 6 to 10!
printf("First number is : %d\n",numbers[0]);
printf("Third number is : %d\n",numbers[2]);
printf("Last number is : %d\n",numbers[4]);
}

Output

TechVidvan Tutorials: Change value of array elements!
First number is : 4
Third number is : 5
Last number is : 10

Example of array using loop:-

#include <stdio.h>
int main () {
int num[10]; // array of 10 elements!
int a,b;
printf("TechVidvan Tutorials: Array using Loop!\n\n");
for ( a = 0; a < 4; a++ ) {
num[a] = a + 20;
}
for (b = 0; b < 4; b++ ) {
printf("Value of Element[%d]: %d\n", b, num[b] );
}
return 0;
}

Output

TechVidvan Tutorials: Array using Loop!

Value of Element[0]: 20
Value of Element[1]: 21
Value of Element[2]: 22
Value of Element[3]: 23

Copying of arrays in C

In C, you can copy the elements of one array to another array.

Example:- Copying arrays in C

#include <stdio.h>
int main(){
float num1[4] = {1.5, 2.6, 3.1, 4.4};
float num2[4];
for (int a = 0; a<4; a++)
{
 num2[a] = num1[a];
}
printf("TechVidvan Tutorials: Copying of an array!\n");
printf("Copying elements of num1 array to num2 array!\n");
for (int a = 0; a<4; a++)
{
printf("Value of Element[%d]: %f\n",a,num2[a]); // printing elements of num2 array!
}
}

Output

TechVidvan Tutorials: Copying of an array!
Copying elements of num1 array to num2 array!
Value of Element[0]: 1.500000
Value of Element[1]: 2.600000
Value of Element[2]: 3.100000
Value of Element[3]: 4.400000

Accessing element out of the size:-

Suppose, you have declared an array of 5 elements like below:-

int num[5];

You can only access the elements from num[0] to num[4].

But let’s say that you want to access the element at num[5]. This will cause errors in your code. Because the element is not available at num[5]. So, do not access elements out of its bound.

Arrays in Detail:-

A programmer should follow the below concepts while using array:-

  • Multi-dimensional Array:- Instead of writing a single array, you can also do a two-dimensional array.
  • Passing arrays to functions:- You can pass an array to a function by specifying the array name.
  • Return array from a function:- With the help of C, you can also return an array from a function.
  • Pointer to an array:- By specifying the name of the array, one can generate a pointer to the first element of the array.

Multi-dimensional Array in C

To put it short, with the help of a multidimensional array, you can create an array of arrays. In multidimensional arrays, data is stored in tabular format. Multidimensional arrays in C are used in computer research and analysis.

Example:- Multidimensional array:-

#include<stdio.h>
int main()
{
// array of 3 rows and 4 columns!
int x[3][4] = {{1,2,-3}, {7,3,8}, {-1,7,5}};
printf("TechVidvan Tutorials: Accessing elements of 2d array!");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++){
printf("Value of Element[%d][%d]: %d\n",i,j,x[i][j]);
}
}
return 0;
}

Output:-

TechVidvan Tutorials: Accessing elements of 2d array!

Value of Element[0][0]: 1
Value of Element[0][1]: 2
Value of Element[1][0]: 7
Value of Element[1][1]: 3
Value of Element[2][0]: -1
Value of Element[2][1]: 7

Passing arrays to function in C:-

You can also pass arrays to a function. Functions help programmers to divide the code into small blocks of codes and with the help of a function, you can execute those blocks of codes again and again.

You can pass an array to a function in two ways:-

1. call by value:- Values of the actual parameters are copied into the function’s formal parameters. And these two types of parameters are stored in different memory locations.

2. call by reference:- In this, the actual and formal parameters refer to the same memory locations. Any changes made into the actual parameter will affect formal parameters. To implement it, we have to use pointers.

Example:- Call by value

In the below example, we pass a single element to the function.

#include <stdio.h>
void just_print(int arr)
{
printf("Value of the 3rd element is: %d\n", arr);
}
int main()
{
printf("TechVidvan Tutorial: Passing a single element to a function!\n\n");
int num[5] = {1, 9, 0, 4, 5};
just_print(num[2]); // passing a single element to the output function!
return 0;
}

Output:-

TechVidvan Tutorial: Passing a single element to a function!

Value of the 3rd element is: 0

In the below example, we pass the entire array to a function.

#include <stdio.h>
void just_print(int num)
{
  printf("%d ", num);
}
int main()
{
printf("TechVidvan Tutorial: Passing the entire array to the function!\n\n");
int i;
int arr[] = {5,6,8,9,6};
for (i=0; i<5; i++)
{
just_print(arr[i]);
}
return 0;
}

Output:-

TechVidvan Tutorial: Passing the entire array to the function!

5 6 8 9 6

Example:- Call by reference
In the below example, we pass an array to the function.

#include <stdio.h>
void just_print(int *num)
{
printf("%d ", *num);
}
int main()
{
printf("TechVidvan Tutorial: Call by reference!\n\n");
int i;
int arr[] = {10, 2, 48, 9};
for (i=0; i<4; i++)
{
just_print(&arr[i]);
}
return 0;
}

Output:-

TechVidvan Tutorial: Call by reference!

10 2 48 9

Pointer to an array in C

You can also pass a pointer to an array. The main purpose of a pointer is that it allows you to access the memory of the variable to which the pointer points to. You can get the value of an entire array with the help of a pointer.

Example:- Pointer to an array

#include<stdio.h>
int main()
{
printf("TechVidvan Tutorial: Pointer to an array!\n\n");
int m;
double arr[] = {4.2, 2.7, 6.2};
double *point;
point = arr;
printf( "Below is the array:\n");
for (m= 0; m < 3; m++ )
{
printf("%0.2f\n", *(point + m) ); // Pointer to an array
}
return 0;
}

Output:-

TechVidvan Tutorial: Pointer to an array!

Below is the array:
4.20
2.70
6.20

Return array from a function in C

You can also return an array from a function. This technique will help you in coding.

Example:- Return array from function

#include <stdio.h>  
int *arr_fuc()  
{  
int arr_name[5];  
printf("Please Enter the elements of the array: ");  
for(int i=0;i<5;i++)  
{  
  scanf("%d", &arr_name[i]);  
}  
return arr_name;  
}  
int main()  
{  
int *point;  
point=arr_fuc();  
printf("TechVidvan Tutorial: Returning array from a function!\n");
printf("Array is: \n");
for(int i=0;i<5;i++)  
{  
  printf("%d", point[i]);  
}  
return 0;  
}

Output:-

Please Enter the elements of the array: 1 2 3 4
TechVidvan Tutorial: Returning array from a function!
Array is: 1 2 3 4

Input/Output Array Elements in C

Example of taking input from the user and storing it in an array element:-

// taking input and storing it in the 2nd element!
scanf("%d", &num[1]);
// taking input and storing it in the nth element!
scanf("%d", &num[n-1]);

Printing individual element of an array:-

// printing the 2nd element of the array!
printf("%d", num[2]);

// printing the nth element of the array!
printf("%d", num[n-1]);

Sorting of arrays in C

You can also sort the arrays in ascending or descending order. It is useful for various search algorithms like Linear search, Merge sort, etc.

Example of sorting an array in C:-

#include<stdio.h>    
void main ()    
{  
printf("TechVidvan Tutorials: Sorting of an array!\n\n");
int a, b,replace;	 
int i[4] = { 5, 6, 9, 10};
printf("Before Sorting..\n");
for(a = 0; a<4; a++){
printf("Value of Element[%d] is %d\n",a,i[a]);
}
for(a = 0; a<4; a++)    
{
for(b = a+1; b<4; b++)    
{    
if(i[b] > i[a])    
{    
replace = i[a]; // Sorting   
i[a] = i[b];	// Sorting
i[b] = replace; // Sorting   
}   
}	 
}
printf("Sorting..\n");
printf("After Sorting..\n");
for(a = 0; a<4; a++)    
{
printf("Value of Element[%d] is %d\n",a,i[a]);    
}    
}

Output

TechVidvan Tutorials: Sorting of an array!

Before Sorting..
Value of Element[0] is 5
Value of Element[1] is 6
Value of Element[2] is 9
Value of Element[3] is 10
Sorting..
After Sorting..
Value of Element[0] is 10
Value of Element[1] is 9
Value of Element[2] is 6
Value of Element[3] is 5

Summary

Array is a collection of elements of the same data type. You can access any element in the array easily. Arrays are fixed in size. You cannot exceed the limit of it. Suppose, you want to store marks of 20 students then you might try to declare 20 variables like student1_marks, student2_marks etc. But what if i tell you that you can do all those with a single variable named array.