Handbook of Computer Science(cs) and IT

Structures

Structures in C are used to encapsulate or group together different data into one object. We can define a structure as given below

struct object

{

char id [20];

int rollno;

int marks;

};

The variables we declare inside the structure are called data member.

Initializing a Structure

Structure members can be initialized when we declare a variable of our structure.

Sample Code

struct object student1 = {“Ram”, 1, 95};

The above declaration will create a struct object called student1 with an id equal to “Ram”, rollno equal to 1 and marks equal to 95. To access the

members of a structure, we use the “.” (dot operator) i.e., scope resolution operator.

 

struct object student1;

student1 . id = “Ram”

student1. roll no = 1 ;

student1. marks = 95;

Key Points

  • The values of a structure variable can be assigned to another structure variable of the same type using assignment operator.
  • One structure can be nested within another structure. Using this facility, complex data types can be created.

e.g., struct emp

{

char name [100];

struct Address a;

);

  • A structure variable can also be passed to a function. We may either pass individual structure elements or the entire structure variable at one go .
  • We can have a pointer pointing to a struct. Such pointers are known as structure pointers.

Unions

A union is a collection of hetrogeneous elements that is, it is a group of

elements which are having different data types. Each member within a

structure is assigned its own memory location. But the union members, all

share a common memory location. Thus, unions are used to save memory.

union person

{

char name [20];        Union definition

i nt age;

float height;

}

 

Like structures, unions are also defined and declared.

union person Ram; // Union declaration

Data Structure

A data structure is a specialised way for organising and storing data in memory, so that one can perform operations on it.

Data structure is all about

  • How to represent data element(s).
  • What relationship data elements have among themselves.
  • How to access data elements e., access methods.

 

Types of Data Structure

Operations on Data Structures

The operations involve in data structure are as follows

Create Used to allocate/reserve memory for the data element(s).

Destroy This operation deallocate/destroy the memory space assigned to the specified data structure.

Selection Accessing a particular data within a data structure.

Update For updation (insertion or deletion) of data in the data structure.

 Searching Used to find out the presence of the specified data item in the list of data item.

Sorting Process of arranging all data items either in ascending or in descending order.

Merging Process of combining data items of two different sorted lists of data items into a single list.

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

Leave a Reply

Your email address will not be published. Required fields are marked *