Arrays in C language
Arrays in C language
An array in C language is a group (or collection) of the same data types. For example, an int array holds the elements of int types while an afloat array holds the elements of float types. An array is a variable that can store multiple values.
How to declare Array in C
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and the type can be any valid C data type.
dataType arrayName[arraySize];
int num[35]; /* An integer array of 35 elements */ char ch[10]; /* An array of characters for 10 elements */
How to access an element of an array in C
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array
double salary = balance[9];
Here is how we write program of arrays in C language
#include <stdio.h> int main() { int avg = 0; int sum =0; int x=0; int num[4]; for (x=0; x<4;x++) { printf("Enter number %d \n", (x+1)); scanf("%d", &num[x]); } for (x=0; x<4;x++) { sum = sum+num[x]; } avg = sum/4; printf("Average of entered number is: %d", avg); return 0; }
Output
Enter number 1 10 Enter number 2 10 Enter number 3 20 Enter number 4 40 Average of entered number is: 20
Types of Array
2D array
We can have multidimensional arrays in C like 2D and 3D arrays. However, the most popular and frequently used array is 2D – two-dimensional array. In this post, you will learn how to declare, read and write data in a 2D array along with various other features.
Program
#include<stdio.h> int main() { int arr[3][3]; //2D array declaration int i,j; printf("enter the elements: \n"); //entering elements for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("elements at [%d][%d]: ",i,j); scanf("%d",&arr[i][j]); } } printf("\nprinting the elements of a 2D array: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d ",arr[i][j]); if(j==2) printf("\n"); } } return 0; }
Output
enter the elements: elements at [0][0]: 1 elements at [0][1]: 2 elements at [0][2]: 3 elements at [1][0]: 4 elements at [1][1]: 5 elements at [1][2]: 6 elements at [2][0]: 7 elements at [2][1]: 8 elements at [2][2]: 9 printing the elements of a 2D array: 1 2 3 4 5 6 7 8 9
Passing an array to a function
You can pass to the function a pointer to an array by specifying the array’s name without an index. Generally, we pass values and variables while calling a function, we can also pass arrays to a function. You can pass the array’s element as well as the whole array to a function.
Program
#include <stdio.h> void disp( char ch) { printf("%c ", ch); } int main() { char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; for (int x=0; x<10; x++) { disp (arr[x]); } return 0; }
Output
a b c d e f g h i j
Pointer to array
Array elements can be accessed and manipulated using Pointer to the array. Using pointers you can easily handle the array. You can have access to all the elements of an array just by assigning the array’s base address to a pointer variable
Program
#include<stdio.h> #include<conio.h> int main() { int marks[6] = {89, 45, 58, 72, 90, 93} ; int *ptr ; clrscr() ; ptr = marks ; printf(“Base Address of 'marks' array = %u\n”, ptr) ; return 0; }
Output
Base Address of 'marks' array = 4356756
Also, read the Arithmetic Expression in C