Skip to main content

Array in C language

Introduction

In this chapter, we will discuss a very important data type arrays. Let us see, “Why arrays?’’
We know that in one variable we can store the information of only one  data item. Suppose that a
Student has scored 90 marks. These marks can be stored in a variable as shown below:

int marks=90;

After executing this statement, the value 90 will be stored in the variable marks. Suppose there is
A need to store marks of 10 students. In such case, we are forced to use 10 variables like marksl,
Marks2… marks10. But, if it is required to store the marks of 100 students, definitely it is not
Feasible to use 100 variables marksl ,marks2…marks100, Now, the question is “How to store 100
different marks?” In mathematics, we use sets to group the items of similar kind, For example,
consider the set shown below :

marks = {80,90,45,99,100,36,88,96,67,92}

This is a set of marks of `10 students. Note that every item can be accessed by prefixing marks along
With the position of marks in the set. The first item 80 corresponds to the marks of first student,90
Corresponds to the marks of second student and so on i.e., marks1=80, marks2=90…marks 10=92.
In C language, this is where, the concept of arrays is used. Since all the marks are of the same type,
We can group and refer all the marks  with a common name using arrays.
Note: In general, if more number of data items of same type (kind) are to be accessed or read
or stored, then we use arrays.


The meaning of an array

Definition : An array is defined as an ordered set of similar data items. All the data items of
an array are stored in consecutive memory locations in main memory . The elements of an array
are of same data type and each item can be accessed using the same name. e.g. consider an
array of marks of 5 students as shown below:

80
90
45
99
100
Marks(0)
Marks(1)
Marks(2)
Marks(3)
Marks(4)

To refer an item in the array, we specify the name of the array along with position of the item. The
Position of the item must be written within square brackets ‘ []’.  The position of the item, The item enclosed within square brackets is called ‘subscript’ or ‘index’.

For example, the above figure represents an integer array called marks where marks of 5 students
Are stored. The marks of each student can be accessed as shown below:

       marks[0] i.e. 80 – represent marks of first student
       marks[ 1] i.e. 90- represent marks of second student
       marks[2] i.e. 45- represent marks of third student
      marks [3] i.e. 99- represent marks of fourth student
       marks[4] i.e. 100 – represent marks of fifth student

Thus using marks [0] through marks[4]  we can access the  marks of 5 students.
Note: Using marks [0] through marks [n-1] We can access the marks of n students in general.
In an array it not possible to have a group of items with different data types. Types. For example,

83
94.8
“Delhi”
‘3’
910
a[0]
a[1]
a[2]
a[3]
a[4]

Is invalid way of storing the elements in an array. This is because, it is a collection of int,  float, char
and string datatypes. Once we know the definition of an array. The next question is “How arrays
are classified?” The arrays can be classified based on how the data items are arranged for human
understanding. This is pictorially represented as shown below:



Comments

Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …