C - Arrays
Lecture Seven
C - Arrays
Arrays a kind of data structure that can store a fixed-size
sequential collection of elements of the same type. An
array is used to store a collection of data, but it is often
more useful to think of an array as a collection of
variables of the same type.
Instead of declaring individual variables, such as
number0, number1, ..., and number99, you declare one
array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual
variables. A specific element in an array is accessed by an
index.
C – Arrays(Cont…)
C Array is a collection of variables belongings to the
same data type. You can store group of data of same
data type in an array.
Array might be belonging to any of the data types
Array size must be a constant value.
Always, Contiguous (adjacent) memory locations are
used to store array elements in memory.
It is a best practice to initialize an array to zero or null
while declaring, if we don’t assign any values to array.
C – Arrays(Cont…)
All arrays consist of contiguous memory locations.
The lowest address corresponds to the first element
and the highest address to the last element
Declaring Arrays
To declare an array in C, a programmer specifies the
type of the elements and the number of elements
required by an array as follows −
type arrayName [ arraySize ];
This is called a single-dimensional array.
The arraySize must be an integer constant greater
than zero and type can be any valid C data type. For
example, to declare a 10-element array
called balance of type double, use this statement −
Initializing Arrays
You can initialize an array in C either one by one or using a
single statement as follows −
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; The
number of values between braces { } cannot be larger than
the number of elements that we declare for the array
between square brackets [ ].
If you omit the size of the array, an array just big enough
to hold the initialization is created. Therefore, if you write
−
Accessing Array Elements
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. For
example −
double salary = balance[9];
The above statement will take the 10th element from
the array and assign the value to salary variable. The
following example Shows how to use all the three
above mentioned concepts viz. declaration,
assignment, and accessing arrays −
Arrays in Detail
Arrays are important to C and should need a lot more
attention.
EXAMPLE FOR C ARRAYS:
int a[10]; // integer array
char b[10]; // character array i.e. string
TYPES OF C ARRAYS:
There are 2 types of C arrays. They are,
One dimensional array
Multi dimensional array
Two dimensional array
Three dimensional array
four dimensional array etc…
Cont…
Conceptually you can think of a one-dimensional
array as a row, where elements are stored one after
another.
Syntax:
datatype array_name[size];
datatype:
It denotes the type of the elements in the array.
array_name:
Name of the array. It must be a valid identifier.
size:
Number of elements an array can hold.
here are some example of array declarations
int num[100];
float temp[20];
char ch[50];
num is an array of type int, which can only store 100 elements of
type int.
temp is an array of type float, which can only store 20 elements
of type float.
ch is an array of type char, which can only store 50 elements of
type char.
Initializing Array
When an array is declared inside a function the
elements of the array have garbage value. If an array is
global or static, then its elements are automatically
initialized to 0. We can explicitly initialize elements of
an array at the time of declaration using the following
syntax:
Syntax:
datatype array_name[size] = { val1, val2, val3, ..... valN };
Cont….
datatype is the type of elements of an array.
array_name is the variable name, which must be any
valid identifier.
size is the size of the array.
val1, val2 … are the constants known as initializers.
Each value is separated by a comma(,) and then there
is a semi-colon (;) after the closing curly brace (}).
Cont….
The simplest way to initialize an array is by using the index
of each element. We can initialize each element of the array
by using the index. Consider the following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Here is are some examples
float temp[5] = {12.3, 4.1, 3.8, 9.5, 4.5}; // an array of 5 floats
int arr[9] = {11, 22, 33, 44, 55, 66, 77, 88, 99}; // an array of 9 ints
While initializing 1-D array it is optional to specify the size of
the array, so you can also write the above statements as:
float temp[] = {12.3, 4.1, 3.8, 9.5, 4.5}; // an array of 5 floats
int arr[] = {11, 22, 33, 44, 55, 66, 77, 88, 99}; // an array of 9 ints
If the number of initializers is less than the specified size then
the remaining elements of the array are assigned a value of 0
float temp[5] = {12.3, 4.1};
Cont…
here the size of temp array is 5 but there are only two
initializers. After this initialization the elements of the
array are as follows:
temp[0] is 12.3
temp[1] is 4.1
temp[2] is 0
temp[3] is 0
temp[4] is 0
Cont….
If the number of initializers is greater than the size of
the array then, the compiler will report an error. For
example:
int num[5] = {1, 2, 3, 4, 5, 6, 7, 8} // error
Multi-dimensional Arrays in C
C programming language allows multidimensional
arrays. Here is the general form of a multidimensional
array declaration −
type name[size1][size2]...[sizeN];
For example, the following declaration creates a three
dimensional integer array −
int threedim[5][10][4];
Properties of Array
The array contains the following properties.
Each element of an array is of same data type and
carries the same size, i.e., int = 4 bytes.
Elements of the array are stored at contiguous memory
locations where the first element is stored at the
smallest memory location.
Elements of the array can be randomly accessed since
we can calculate the address of each element of the
array with the given base address and the size of the
data element.
Advantage of C Array
1) Code Optimization: Less code to the access the
data.
2) Ease of traversing: By using the for loop, we can
retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we
need a few lines of code only.
4) Random Access: We can access any element
randomly using the array
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of
declaration of the array, we can't exceed the limit. So,
it doesn't grow the size dynamically like LinkedList
which we will learn later.