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

Lecture 6 Arrary

The document discusses arrays in C language, including how they are declared and initialized at compile time or runtime. It also covers multidimensional arrays and provides examples of declaring and initializing one and two dimensional arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture 6 Arrary

The document discusses arrays in C language, including how they are declared and initialized at compile time or runtime. It also covers multidimensional arrays and provides examples of declaring and initializing one and two dimensional arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C Arrays

In C language, arrays are referred to as structured data types. An array is defined as finite ordered
collection of homogenous data, stored in contiguous memory locations.
Here the words,

 finite means data range must be defined.


 ordered means data must be stored in continuous memory addresses.
 homogenous means data must be of similar data type.

Example where arrays are used,

 to store list of Employee or Student names,


 to store marks of a students,
 or to store list of numbers or characters etc.

Since arrays provide an easy way to represent data, it is classified amongst the data structures in
C. Other data structures in c are structure, lists, queues and trees. Array can be used to represent
not only simple list of data but also table of data in two or three dimensions.

Declaring an Array
Like any other variable, arrays must be declared before they are used. General form of array
declaration is,
data-type variable-name[size];
for example :
int arr[10];

Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-1 i.e
first element of arr array will be stored at arr[0] address and last element will occupy arr[9].
Initialization of an Array
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any
random value). An array can be initialized at either compile time or at runtime.

Compile time Array initialization


Compile time initializtion of array elements is same as ordinary variable initialization. The
general form of initialization of array is,

One important things to remember is that when you will give more initializer than declared array
size than the compiler will give an error.
Runtime Array initialization

An array can also be initialized at runtime using scanf() function. This approach is usually used
for initializing large array, or to initialize array with user specified values. Example,

Two dimensional Arrays


C language supports multidimensional arrays. The simplest form of the multidimensional array
is the two-dimensional array.
Two-dimensional array is declared as follows,
The above array can also be declared and initialized together. Such as,
int arr[][3] = {
{0,0,0},
{1,1,1}
};
Run-time initialization of two dimensional Array

You might also like