0% found this document useful (0 votes)
92 views

Arrays

The C language provides a capability that enables the user to design a set of similar data types,called array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Arrays

The C language provides a capability that enables the user to design a set of similar data types,called array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Arrays

C Programming Arrays:
In C programming, one of the frequently arising problem is to
handle similar types of data.
For example: If the user wants to store marks of 100 students.
This can be done by creating 100 variables individually but, this
process is rather tedious and impracticable. These types of
problem can be handled in C programming using arrays.
An array is a sequence of data item of homogeneous values(same
type).
Arrays are of two types:
One-dimensional arrays
Multidimensional arrays
Declaration of one-dimensional array:
data_type array_name[array_size];
For example:
int age[5];
Here, the name of array is age. The size of array is 5 ,i.e., there are 5
items(elements) of array age. All the elements in an array are of the
same type (int, in this case).

Array elements:
Size of array defines the number of elements in an array. Each element
of array can be accessed and used by user according to the need of
program. For example:
int age[5];

Note that, the first element is numbered 0 and so on.


Here, the size of array age is 5 times the size of int because there are 5
elements.
Suppose, the starting address of age[0] is 2120d and the size of int be 4
bytes. Then, the next address (address of a[1]) will be 2124d, address of
a[2] will be 2128d and so on.
Initialization of one-dimensional array:
Arrays can be initialized at declaration time in this source code as:
int age[5]={2,4,34,3,4};
It is not necessary to define the size of arrays during initialization.
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the
number of elements of an array.

Accessing array elements:


In C programming, arrays can be accessed and treated like variables in
C.
For example:
scanf("%d",&age[2]);
/* statement to insert value in the third element of array age[]. */
scanf("%d",&age[i]);
/* Statement to insert value in (i+1)th element of array age[]. */
/* Because, the first element of array is age[0], second is age[1], ith is
age[i-1] and (i+1)th is age[i]. */
printf("%d",age[0]);
/* statement to print first element of an array. */
printf("%d",age[i]);
/* statement to print (i+1)th element of an array. */
Example of array in C programming:
/* C program to find the sum marks of n students using arrays */
#include <stdio.h>
int main(){
int marks[10],i,n,sum=0;
printf("Enter number of students: ");

scanf("%d",&n);
for(i=0;i<n;++i){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Output:
Enter number of students: 3
Enter marks of student1: 12
Enter marks of student2: 31
Enter marks of student3: 2
sum=45
Important thing to remember in C arrays
Suppose, you have declared the array of 10 students. For example:
arr[10]. You can use array members from arr[0] to arr[9]. But, what if
you want to use element arr[10], arr[13] etc. Compiler may not show
error using these elements but, may cause fatal error during program
execution.
4

C Programming Multidimensional Arrays:


C programming language allows programmer to create arrays of arrays
known as multidimensional arrays.
For example:
float a[2][6];
Here, a is an array of two dimension, which is an example of
multidimensional array. This array has 2 rows and 6 columns
For better understanding of multidimensional arrays, array elements of
above example can be thought of as below:

Initialization of Multidimensional Arrays:


In C, multidimensional arrays can be initialized in different number of
ways.
int c[2][3]={{1,3,0}, {-1,5,9}};
OR
int c[][3]={{1,3,0}, {-1,5,9}};
OR
int c[2][3]={1,3,0,-1,5,9};

Initialization Of three-dimensional Array:


double cprogram[3][2][4]={
{{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},
{{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},
{{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}
};
Suppose there is a multidimensional array arr[i][j][k][m]. Then this array
can hold i*j*k*m numbers of data.
Similarly, the array of any dimension can be initialized in C
programming.
Accessing arrays:
We have following array which is declared like this int arr[] = { 51,32,43,24,5,26};
As we have elements in an array, so we have track of base address of
an array. Below things are important to access an array
Expression

Description

Example

arr

It returns the base address of an array

*arr

It gives zeroth element of an array

51

*(arr+0)

It also gives zeroth element of an array

51

*(arr+1)

It gives first element of an array

32

Consider 2000

So whenever we tried accessing array using arr[i] then it returns an


element at the location *(arr + i)
Accessing array a[i] means retrieving element from address (a + i).
arr[i] = 5
*(arr+i) = 5
*(i+arr) = 5
i[arr] = 5
All of the above notations yield same result.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[] = {51,32,43,24,5,26};
int i;
for(i=0; i<=5; i++) {
printf("\n%d %d %d %d",arr[i],*(i+arr),*(arr+i),i[arr]);
}
getch();
}
7

Output :
51 51 51 51
32 32 32 32
43 43 43 43
24 24 24 24
5555
26 26 26 26
C Array Limitations:
Static Data:
o Array is Static data Structure.
o Memory Allocated during Compile time.
o Once Memory is allocated at Compile Time it cannot be
changed during Run-time.
Can hold data belonging to same Data types
Elements belonging to different data types cannot be stored
in array because array data structure can hold data belonging
to same data type.
Example : Character and Integer values can be stored inside
separate array but cannot be stored in single array.

Inserting data in Array is Difficult


Inserting element is very difficult because before inserting
element in an array we have to create empty space by shifting
other elements one position ahead.
This operation is faster if the array size is smaller, but same
operation will be more and more time consuming and nonefficient in case of array with large size.
Deletion Operation is difficult
Deletion is not easy because the elements are stored in
contiguous memory location.
Like insertion operation , we have to delete element from the
array and after deletion empty space will be created and thus we
need to fill the space by moving elements up in the array.
Bound Checking
If we specify the size of array as N then we can access elements
upto N-1 but in C if we try to access elements after N-1 i.e Nth
element or N+1th element then we does not get any error
message.
Process of Checking the extreme limit of array is called Bound
Checking and C does not perform Bound Checking.
If the array range exceeds then we will get garbage value as
result.
Shortage of Memory
Array is Static data structure. Memory can be allocated at compile
time only Thus if after executing program we need more space for
9

storing additional information then we cannot allocate additional


space at run time.
Shortage of Memory, if we dont know the size of memory in
advance.
Wastage of Memory
Wastage of Memory, if array of large size is defined

10

You might also like