Chap 7
Chap 7
Chap 7
Introduction
• An array in C is a fixed-size collection of similar data items stored in
contiguous memory locations. It can be used to store the collection of
primitive data types such as int, char, float, etc.
• Some examples where the concept of arrays can be used:
• List of students in a class
• List of test scores of a class of students
• List of temperature recorded every hour a day
• List of employees in an organization. Etc…
• Since an array provides a convenient structure for representing data,
it is classified as one of the data structures in C.
• The ability to use a single name to represent a collection of items and
to refer to an item by specifying the item number enables us to
develop concise and efficient programs.
• Arrays do not only represent single list of items but also table of data
in two, three or more dimensions.
• Accordingly array are categorized as:
• One dimensional array
• Two dimensional array
• Multidimensional array
One Dimensional Array
• A list of items can be given one variable name using only one subscript
and such a variable is called a one dimensional array.
• C Array Declaration
• In C, the array must be declared like any other variable before using it.
• An array can be declared by specifying its name, the type of its elements,
and the size of its dimensions.
• When an array is declared in C, the compiler allocates the memory block
of the specified size to the array name.
• Syntax for declaring array
data_type array_name [size];
• Consider the following array declaration:
1. float height[50];
2. int group[10];
3. char name[10];
• Explain each declaration.
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
C Array Initialization
• Initialization in C is the process to assign some initial value to the
variable.
• When the array is declared or allocated memory, the elements of the
array contain some garbage value.
• So, we need to initialize the array to some meaningful value.
• An array can be initialized at either of the following stages:
• At compile time
• At run time
Compile time initialization
• In compile time initialization, there are multiple ways:
1. Array Initialization with Declaration
• In this method, we initialize the array along with its declaration.
• An initializer list is used to initialize multiple elements of the array.
• An initializer list is the list of values enclosed within braces { }
separated by a comma.
data_type array_name [size] = {value1, value2, ... valueN};
2. Array Initialization with Declaration without Size
• If an array is initialized using an initializer list, then declaring the size
of the array can bbe skiped as the compiler can automatically deduce
the size of the array in these cases.
• The size of the array in these cases is equal to the number of
elements present in the initializer list as the compiler can
automatically deduce the size of the array.
data_type array_name[] = {1,2,3,4,5};
• The size of the above arrays is 5 which is automatically deduced by
the compiler.
Run time Initialization
• The array can be initialized after the declaration by assigning the
initial value to each element individually.
• For loop, while loop, or do-while loop can be used to assign the value
to each element of the array.