Array in C Language
Featured snippet from the web
C - Arrays
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. ... A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations.
2. Definition
An array is a data structure which
stores homogeneous(similar)data
items.
An array variable is used to store
more than one data item of same data
type at contiguous memory locations.
3. Example:
C array is beneficial if you have to store
similar elements.
Suppose you have to store marks of 50
students, one way to do this is allotting 50
variables.
So it will be typical and hard to manage.
For example we can not access the value
of these variables with only 1 or 2 lines of
code.
Another way to do this is array. By using
array, we can access the elements easily.
Only few lines of code is required to
access the elements of array.
4. Advantage of C Array
1) Code Optimization: Less code to
the access the data.
2) Easy to traverse data: By using the
for loop, we can retrieve the elements
of an array easily.
3) Easy to sort data: To sort the
elements of array, we need a few lines
of code only.
4) Random Access: We can access
any element randomly using the array.
5. Disadvantage of C Array
1) Fixed Size: Whatever size, we define
at the time of declaration of array, we
can't exceed the limit. So, it doesn't
grow the size dynamically like
LinkedList
6. Array Terminology
Array variable is composed with the following
terms:
Size:total number of elements in an array
Type: data type of declared array.
Base:address of the first element of the
array.
Index:Location or index value of array
element.
Range:Value from lower bound to upper
bound.
Word:indicates the space required for an
element.
7. Declaration of C Array
when array size is given:
We can declare an array in the c
language in the following way:
the example to declare array:
Here, int is the data_type, marks is
the array_name and 5 is
the array_size.
8. Declaration of C Array
when array size is not known before
compilation:
data type array_name [];
//size will depend upon the elements
entered by user at run time.
Example:
int a[];
9. Initialization of C Array
At the time of declaration:
A simple way to initialize array is by
index.
array index starts from 0 and ends
with [SIZE - 1].
int
a[5]={10,20,30,40,50}
10. Taking input from the user:
void main()
{
int a[5];
for(int i=0;i<5;i++)
{ to take input
printf(“enter the array element”); from the
user
scanf(“%d”,&a[i]);
}
for(int i=0;i<5;i++)
{ to display
output
printf(“the array is: %d”,a[i]); to the user
}
}
12. C Array: Declaration with
Initialization
We can initialize the c array at the
time of declaration.
In such case, there is no requirement
to define size. So it can also be
written as the following code.
14. Two dimensional Array
The two dimensional array in C
language is represented in the form of
rows and columns, also known as
matrix.
It is also known as array of
arrays or list of arrays.
The two dimensional, three
dimensional or other dimensional
arrays are also known
as multidimensional arrays.
15. Declaration of two dimensional
Array in C
We can declare an array in the C
language in the following way:
A simple example to declare two
dimensional array:
Here, 4 is the row number and 3 is
the column number.