Chapter - 1
Manoj Nagar ( 9929309250) 3
Arrays in C
How to create an array:
Creation of an consists two things: Element Type and Array Size.
Element Type: What kind of data an array can hold?
An array can hold any one of the following data:
integer, double, character data.
Array Size: How many elements an array can contain?
Once an array size is defined it cannot be changed at run-time
Using arrays in C:
In C, arrays can be classified based on how the data items are arranged for human understanding. Arrays are
broadly classified into three categories,
1. One Dimensional Arrays
2. Two Dimensional Arrays
3. Multi Dimensional Arrays
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 4
Arrays in C
One Dimensional Arrays:
One dimensional array is a linear list consisting of related and similar Data items.
In memory all the data items are stored in contiguous memory locations one after the other.
Syntax for declaring One Dimensional Arrays:
elementType arrayName[size];
Where elementType specifies data type of each element in the array,arrayName specifies name of the
variable you are declaring and size specifies number of elements allocated for this array.
To declare regular variables we just specify a data type and a unique name.
Example: int number;
To declare an array, we just add an array size.
Example: int temp[5]; //Creates an array of 5 integer elements.
Example: double stockprice[31]; //Creates an array of 31 double elements.
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 5
Arrays in C
Initializing One Dimensional Arrays:
If array is not initialized it contain garbage values.
Types of array initializations:
Option 1: Initializing all memory locations
Option 2: Initialization without size
Option 3: Partial array initialization
Option 4: Initializing an entire array with zero.
Option 1: Initializing all memory locations:
If you know all the data at compile time, you can specify all your data within brackets:
int temp [5] = {75, 79, 82, 70, 68};
During compilation, 5 contiguous memory locations are reserved by the compiler for the variable temp and
all these locations are initialized as shown below.
If the size of integer is 2 bytes, 10 bytes will be allocated for the variable temp.
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 6
Arrays in C
Option 2: Initialization without size:
If you omit the size of an array, but specify an initial set of data, then the compiler will automatically
determine the size of an array.
int temp [] = {75, 79, 82, 70, 68};
In the above declaration, even though you have not specified exact number of elements to be used in array
temp, the array size will be set with the total number of initial values specified.
Here, the compiler creates an array of 5 elements. The array temp is initialized as shown below.
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 7
Arrays in C
Option 3 Partial Array Initialization:
If the number of values to be initialized is less than the size of the array, then the elements are initialized in
the order from 0th location.
The remaining locations will be initialized to zero automatically.
int temp [5] = {75, 79, 82};
Even though compiler allocates 5 memory locations, using the above declaration statement, the compiler
initializes first three locations with 75, 70 and 82, and the next set of memory locations are automatically
initialized to 0’s by the compiler as shown below.
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 8
Arrays in C
Option 4: Initializing an entire array with zero:
If you do not know any data ahead of time, but you want to initialize everything to 0, just use 0 within { }. For
example:
int temp [5] = {0};
This will initialize every element within the array to 0 as shown below.
Example:
int temp [5] = {5};
The first value is supplied in the first element memory location, remaining all elements are placed with zero.
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 9
Arrays in C
Accessing elements of one dimensional array:
You know how to declare and initialize an array. Now lets understand, how to access an array elements.
To access an array element use name of the array with the subscript in brackets.
Suppose you have an array called temperature, for storing temperature in a year.
Then the subscripts would be 0,1,…,364.
For example to access temperature of fifth day:
temperature [4]
To assign or store the value 89 for the 150th day:
temperature [149] = 89
You can loop through the elements of an array by varying the subscript.
To set all of the values to 0, say
for(i=0;i<365;i++)
temperature[i] = 0;
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 10
Arrays in C
You can not use assignment statement directly with arrays.
If a[] , b[] are two arrays then the assignment a=b is not valid. //Example for accessing array elements.
C does not provide array bounds checking. #include<stdio.h>
main() {
Hence, if you have
int arr[10];
double stockPrice[5]; int i = 0;
printf ("%d", stockPrice[10]); for(i=0;i<sizeof(arr);i++)
This will compile, but you have overstepped the bounds of your array. {
You may therefore get wrong value. arr[i] = i;
As we can see above, the 5th element of an array is accessed as ‘arr[5]‘. }
int j = arr[4];
Note that for an array declared as int arr[5].
printf(“Value at 5th location is: %d”,j);
The five values are represented as: arr[0] arr[1] arr[2] arr[3] arr[4] }
and not arr[1] arr[2] arr[3] arr[4] arr[5]
The first element of array always has a subscript of ’0′.
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 11
Arrays in C
//Program to calculate sum of all the array elements.
#include <stdio.h>
void main()
{
int a[10];
int i, size, total=0;
printf(“ Enter the size of the array : ");
scanf("%d", &size);
printf(“ Enter the elements of an array : ");
for (i = 0; i < size ; i++)
scanf("%d",&a[i]);
for (i = 0; i < size ; i++)
total += a[i];
printf(“Sum of all array elements: %d", total);
}
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 12
Arrays in C
Two Dimensional Arrays:
Two-dimensional array are those type of array, which has finite number of rows and finite number of
columns.
An array of array is called a two-dimensional array and can be represented as a table with rows and
columns.
This is an array of size 3 names whose elements are
arrays of size 4.
Syntax:elementType arrayName [rowsize][columnsize];
Two-dimensional Array representation
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 13
Arrays in C
The declaration form of 2-dimensional array is
elementType arrayName [row size][column size]; [0] [1] [2]
The elementType may be any valid type supported by C.
The rule for giving the arrayName is same as the ordinary variable. [0] 10
The row size and column size should be an individual constant.
The following declares a two-dimensional 3 by 3 array of integers and [1]
sets the first and last elements to be 10.
int matrix [3][3]; [2] 10
matrix[0][0] = 10;
matrix[2][2] = 10;
In the declaration of two dimensional array the column size should be specified, so that it can arrange the
elements in the form of rows and columns.
Two-dimensional arrays in C are stored in "row-major format": the array is laid out contiguously, one row at a
time.
Two-dimensional Array representation
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 14
Arrays in C
Initialization of Two Dimensional Arrays:
An array may be initialized at the time of declaration as follows:
char names [3][4] = {
{‘J’, 'o', 'h', 'n'},
{‘M’, 'a', 'r', 'y'},
{‘I’, 'v', 'a', 'n'}
};
An integer array may be initialized to all zeros as follows
int nums [3][4] = {0};
An integer array may be initialized to different values as follows
int nums [3][4] = {
{1,2,3,4},
{5,6,7,8},
{9,0,10,11}
};
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 15
Arrays in C
To access an element of a 2D array, you need to specify both the row and the column:
printf ("%d", nums[1][2]); //Program to print sum of elements of a matrix.
#define M 3 /* Number of rows */
#define N4 /* Number of columns */
main() {
int a [ M ] [ N ], i, j, sum = 0;
for ( i = 0; i < M; ++i ) {
for (j = 0; j < N, ++j ){
scanf (%d”, &a [i] [j]);}
}
for ( i = 0; i < M; ++i ) {
for (j = 0; j < N, ++j ) {
printf(“a [ %d ] [ %d ] = %d “, i, j, a[ i ] [ j ]);
}
printf (“\n”);
}
for ( i = 0; i < M; ++i ) {
for (j = 0; j < N, ++j ) {
sum += a[ i ] [ j ];}
printf(“\nsum = %d\n\n”);
}
}
Manoj Nagar ( 9929309250) Unit-2 Arrays, Strings, Structures and Pointers 16