In C programming, an array is a collection of elements of the same data type, stored in contiguous memory locations.Arrays is used to store multiple values in a single variable. The elements in an array can be accessed by their index or position within the array.Array Always Start with index number 0 . define the data type (like int
) and the name of the array followed by ” square brackets [] “.
.Example of Array :-
int myArray[ ] = {1, 2, 3, 4, 5};
In C programming, there are several types of arrays, including one-dimensional arrays, multi-dimensional arrays, and arrays of pointers. Here are some examples of each type of array in tabular format:
The way of array Declaration And Initialize :-
1.Declare array :- int a[5];
Initialize the value of Array by index .
a[ 0 ]=10; a[ 1 ]=20; a[ 2 ]=30; a[ 3 ]=40; a[ 4 ]=50;
2. array Declare and initializetion at one time :- int a[ ] = {5,10,15,20,25,30};
Type of array :-
A. One – Dimentional Array.
B. Multi – Dimentional Array.
1. One-dimensional array :-
A one-dimensional array is a collection of elements of the same data type, arranged in a single row.
// Example of a one-dimensional array of integers.
#include <stdio.h>
int main()
{
int n[5] = {101, 102, 103, 104, 105};
for(int i=0; i<5; i++)
{
printf(“n[%d] = %d\n”, i, n[i]);
}
return 0;
}
Output:-
n[0] = 101
n[1] = 102
n[2] = 103
n[3] = 104
n[4] = 105
Explain Output:-
In the Given example, we have declared a 1D array of integers n. The array contains five elements, and each element is initialized with a value like output give below.Hare we have print the value of array with using of for loop.
Index | 0 | 1 | 2 | 3 | 4 |
Value | 101 | 102 | 103 | 104 | 105 |
Multi-dimensional array :-
A multi-dimensional array is an array that contains one or more arrays as its elements. In C programming, a two-dimensional array is the most common type of multi-dimensional array.
// Example of a two-dimensional array of integers
#include <stdio.h>
int main()
{
int n[3][3] = {{01,02,03},{04,05,06},{07,08,09}};
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
printf(” %d \t”,n[i][j]);
}
printf(“\n”);
}
return 0;
}
Output :-
01 02 03
04 05 06
07 08 09
Array of pointers :-
An array of pointers is an array where each element is a pointer to another data type.In simple words, the array is capable of holding the addresses a variables. Think of it like this- the array[0] will hold the address of one integer variable, then the array[1] will hold the address of the other integer variable.
// Example of an array of pointers to integers.
Example with Array to array
#include <stdio.h>
main() {
int
*pointer_array[5];
int
array[] = {10, 20, 30, 40, 50};
* Assigning addresses of integer array elements */
pointer_array[0] = &array[0];
pointer_array[1] = &array[1];
pointer_array[2] = &array[2];
pointer_array[3] = &array[3];
pointer_array[4] = &array[4];
for
(int i=0; i<5; i++)
{
printf
(
"%d\n"
, *pointer_array[i]);
}
return
0;
}
Output :-
10 20 30 40 50Example With Array to variables
#include <stdio.h>
int
main() {
int
*pointer_array[5];
int a=5,b=10,c=15,d=20;
pointer_array[0] = &a;
pointer_array[1] = &b;
pointer_array[2] = &c;
pointer_array[3] = &d;
f
or
(int i=0; i<4; i++)
{
printf
(
"%d\n"
, *pointer_array[i]);
}
return
0;
}
Output :-
5 10 15 20
Index | 0 | 1 | 2 3 |
Value | 5 | 10 | 15 20 |
Note :- that in the above Example we create pointer-array contains the memory addresses of the variables a, b, and c,d. when we print array value by pointer.