Introduction To Arrays
Introduction To Arrays
Introduction To Arrays
In C programming, one of the frequently problem is to handle similar types of data. For example: if the
user wants to store marks of 500 students, this can be done by creating 500 variables individually but,
this is rather tedious and impracticable. These types of problem can be handled in C programming using
arrays.
An array in C Programing can be defined as number of memory locations, each of which can store the
same data type and which can be references through the same variable name. It is a collective name
given to a group of similar quantities. These similar quantities could be marks of 500 students, number
of chairs in university, salaries of 300 employees or ages of 250 students. Thus we can say array is a
sequence of data item of homogeneous values (same type). These values could be all integers, floats or
characters etc.
1. One-dimensional arrays.
2. Multidimensional arrays.
data_type array_name[array_size];
Here “data_type” is the type of the array we want to define, “array_name” is the name given to the
array and “array_size” is the size of the array that we want to assign to the array. The array size is always
mentioned inside the “[]”.
For example:
Int age[5];
Initializing Arrays
Initializing of array is very simple in c programming. The initializing values are enclosed within the curly
braces in the declaration and placed following an equal sign after the array name. Here is an example
which declares and initializes an array of five elements of type int. Array can also be initialized after
declaration. Look at the following code, which demonstrate the declaration and initialization of an array.
int age[5]={2,3,4,5,6};
int age[]={2,3,4,5,6};
In this case, the compiler determines the size of array by calculating the number of elements of an array.
For example:
scanf("%d",&age[2]);
//statement to insert value in the third element of array age[]
printf("%d",age[2]);
//statement to print third element of an array.
Arrays can be accessed and updated using its index.An array of n elements, has indices ranging from 0 to
n-1. An element can be updated simply by assigning
A[i] = x;
A great care must be taken in dealing with arrays. Unlike in Java, where array index out of bounds
exception is thrown when indices go out of the 0..n-1 range, C arrays may not display any warnings if out
of bounds indices are accessed. Instead,compiler may access the elements out of bounds, thus leading
to critical run time errors.
Example of array in C programming
#include <stdio.h>
int main(){
int i,n;
int marks[n];
int sum=0;
for(i=0;i<n;i++){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]); //saving the marks in array
sum+=marks[i];
}
return 0;
Output :
Sum of marks = 50
int smarks[3][4];
Here, smarks is an array of two dimension, which is an example of multidimensional array. This array has
3 rows and 4columns. For better understanding of multidimensional arrays, array elements of above
example can be as below:
Make sure that you remember to put each subscript in its own, correct pair of brackets. All three
examples below are wrong.
This program asks user to enter the size of the matrix (rows and column) then, it asks the user to enter
the elements of two matrices and finally it adds two matrix and displays the result.
int main(){
int r,c;
int a[r][c];
int b[r][c];
int sum[r][c;
for(int i=0;i<r;++i){
for(int j=0;j<c;++j){
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<r;++i)
for(int j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
return 0;
Program Output:
Enter number of rows (between 1 and 100): 3
Enter number of rows (between 1 and 100): 2
8 -11
17 6
5 5
Dynamic Arrays and Resizing
Arrays by definition are static structures, meaning that size cannot be changed during run time. When
an array is defined as
int A[n];
then A is considered a static array and memory is allocated from the run time stack for A. When A goes
out of scope, the memory is deallocated and A no longer can be referenced. C allows dynamic
declaration of an array as follows:
The above code declares a memory block of size n*sizeof(int) that can be accessed using the pointer A.
For example, A can be initialized as follows:
int i;
for (i=0; i<n; i++)
A[i] = 0;
Note that although A was declared as a pointer, A can be treated as an array. The difference between
int A[10] and int* A = malloc(10*sizeof(int)) is that latter is assigned memory in the dynamic heap (and
hence must be managed by the programmer) and former is assigned memory from the run time stack
(and hence managed by the compiler). Static arrays are used when we know the amount of bytes in
array at compile time while the dynamic array is used where we come to know about the size on run
time. Arrays can also be initialized using the calloc() functions instead of the the malloc(). Calloc function
is used to reserve space for dynamic arrays. Has the following form.
array =(cast-type*)calloc(n,element-size);
Number of elements in the first argument specifies the size in bytes of one element to the second
argument. A successful partitioning, that address is returned, NULL is returned on failure. For example,
an int array of 10 elements can be allocated as follows.
Output
4
Single element of an array can be passed in similar manner as passing variable to a function.
C program to pass an array containing age of person to a function. This function will return average
age and display the average age in main function.
#include <stdio.h>
float average(float a[]);
int main(){
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c); /* Only name of array is passed as argument. */
printf("Average age=%.2f",avg);
return 0;
}
float average(float a[]){
int i;
float avg, sum=0.0;
for(i=0;i<6;++i){
sum+=a[i];
}
avg =(sum/6);
return avg;
}
Output
Average age=27.08
Output
Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5