Arrays in C or Concept of Array in C language
Array is a very important concept while Learning C Programming. An array allows you to store and work with multiple values of the same data type. Learning C Programming is not completed without the concept of Array.
Arrays Hold Multiple Values
The array can hold multiple values and can be used consecutive space in the memory. It is one of the major drawbacks of the array but it also has benefits.
Benefits of Array in C:
- Arrays represent multiple data items of the same type using a single name.
- In arrays, the elements can be accessed randomly by using the index number.
- Arraysallocate memory in contiguous memory locations for all its elements.
Drawbacks of Array in C:
- Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
- Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.
How to Access Array Elements in C
The elements of the array can be assessed using loops i.e mostly we use for loop for this purpose. The individual elements of an array are assigned unique subscripts. These subscripts are used to access the elements. Subscript numbering in C always starts at zero. The subscript of the last element in an array is one less than the total number of elements in the array.
An Example of an Array
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
How to pass Arrays as Function Arguments in C?
C does not allow to pass an entire array as an argument to a function. However, you can pass a pointer to an array by specifying the array’s name without an index.
If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameters.
The basic syntax for passing the array to function
void my_Function(int param[10]) {
.
.
.
}
Arrays holding multiple values
#include <stdio.h>
void display(int age1, int age2)
{
printf("%d\n", age1);
printf("%d\n", age2);
}
int main()
{
int ageArray[] = {2, 8, 4, 12};
// Passing second and third elements to display()
display(ageArray[1], ageArray[2]);
return 0;
}
Output on screen:
8
4
Two-Dimensional Arrays in C
A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data.
The declaration of 2d array looks like this in the following table below:
We use nested for loop generally to initialize 2d arrays.
Example of two-dimensional array in C
#include
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
Output on Screen:
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
1 2 3
4 5 6