Array in C Language or in other programming languages is a collection or group of the same data-type elements. Elements can be numbers or digits, characters, or strings.
I think you understand very well with a one-line definition of an array in C.
I assumed that you know what is data-type in C programming? If you do not know about it, don’t jump on Array. First, Learn the basics of C Programming.
Table of Contents
How many types of arrays are in C Language?
Array are of two types:
(1) One-Dimensional or 1-d Array
(2) Two-Dimensional or 2-d Array
In a one-dimensional array, data elements are stored in a single row continuously. While in the two-dimensional array, data elements are stored in row and column metrics as shown in the image.
In this article, we will explain an array using a 1-dimensional array.
How to Declare an Array in C Language?
Here, I am going to write the syntax for the declaration of an array:
data_type name_of_array[Number_of_Elements]
A data type can be int, char, string, etc. You can choose any name to declare an array. The number of elements means how many elements you want to declare.
Let’s understand it with an example,
int My_array[5];
You can also write a positive constant expression inside the square bracket,
int My_array[6/3];
int My_array[5*5];
How to initialize an array in C programming?
There are many ways to initialize an array in C,
Way-1: int My_array[5]={1,2,3,4,5};
Way-2: Don’t define any particular number inside the bracket like, int My_array[]={1,2,3,4,5};
If you define the number of elements inside the square bracket, 5 memory locations will be reserved. Suppose, you define only 3 elements instead of 5 elements (inside the curly brackets), then the remaining 2 memory locations will be wasted.
You cannot increase the Array length after defining the length inside the square bracket.
if you do not define the length inside the square bracket then you can easily increase or decrease the elements.
Way-3: Using Macro
#include <stdio.h>
#define L 5
{
int My_array[L]={1,2,3,4,5};
}
How to Access Array in C Language?
int My_array[3]={1,2,5};
where 3 (written in the square bracket) is the length of the array.
If you want to access the array then, My_array[index];
The value of the index starts from 0 to length-1.
My_array[0] = 1;
My_array[1] = 2;
My_array[2] = 5;
How to print an array in C Language?
Use “Printf” to print an Array in C Language. For example,
#include <stdio.h>
int main()
{
int My_array[5]={1,2,3,4,5}; //initialize Array in C language
int i;
For (i=0; i<=5;i++)
{
printf (“My_array[i] = %d”, My_array[i]);
}
return 0;
}
How to know the length of array in C?
To calculate the length of an array, divide the size of the array by the size of the data type. Let’s understand it with an example,
#include <stdio.h>
int main()
{
//Initialize Array
int My_array[5]={1,2,3,4,5};
int length, M, N;
printf (“Size of My_array=%d\n”, M=Sizeof(My_array));
printf (“Size of data_type int=%d\n”, N = Sizeof(int));
length = M/N; //Calculate Length
printf(“length=%d\n”,length);
return 0;
}
Before this, we read what Array of Pointer is. Before that, it is important that you know what an array is and what is a pointer?
What is an array of pointers in C Language?
The array of Pointer is nothing but an array of pointer variables. Let’s understand the Array of Pointer with an example,
#include <stdio.h>
int main()
{
int My_array[5]={1,2,3,4,5}; //Initialize array
int *p[5];//Declare Pointer “p”
Note: Keep in mind that the data type of the pointer and the data type of the array should be the same.int i;
#include <stdio.h>
int main()
{
int My_array[5]={1,2,3,4,5}; //Initialize array
int *p[5];//Declare Pointer “p”
int i;
for (i=0;i<5;i++)
{
p[i]=&My_array[i];//Address of array assigned to pointer “p”
}
for (i=0;i<5;i++)
{
printf(“Array of pointer=%d\n”,*p[i]);
}
return 0;
}
Output
Array of pointer=1
Array of pointer=2
Array of pointer=3
Array of pointer=4
Array of pointer=5
How to pass an array to a function in C Language?
There are two ways to pass an array to a function: call by value and call by reference.
Pass an array using call by value method
In this method, pass the value of an array to a function.
#include <stdio.h>
int print(value); //Declaration of function ‘print’.
int main()
{
int My_array[5]={1,2,3,4,5}; //Initialization of Array in C Language
int x,i;
for (i=0;i<5;i++)
{
x=print(My_array[i]); //Calling Function ‘print’
}
return 0;
}
//Definition of Function ‘print’
int print(int arr)
{
printf(“value=%d\n”,arr);
}
Output
value=1
value=2
value=3
value=4
value=5
Pass an array using call by reference method
In this method, the address of the array is passed instead of the values of the array.
#include <stdio.h>
int print(Address); //Function Declaration ‘print’
int main()
{
int My_array[5]={1,2,3,4,5};//Array initialization
int *Address, i;
for (i=0;i<5;i++)
{
print (&My_array[i]);//Calling Function ‘print’
}
return 0;
}
//Function Definition ‘Print’
int print(int *y)
{
printf (“value=%d\n”,*y);
}
Output
value=1
value=2
value=3
value=4
value=5