Data Structures:

As we have seen that a variable can hold one item or one particular value at a time. Sometimes we need to operate different types of variables together as a unit. In such a situation, arrays and variables are not both are not sufficient. Therefore, we introduce a new concept “Data Structures”, as the name says that data structure holds different variables of different types.

For example if we want to store information about a book that is the name of the book, author of the book, publisher, price, title, etc we need to store them together in this case you need to define separate variables to contain each of the information or parameters that you need to describe the book, but you will also desire that the whole information/data is grouped together and should have the data type BOOK. For this purpose structures are used.

 

Define a structure

Let us again consider the example of book. Let suppose that we want to include all information of the book that is title, author, publisher, price etc. within our definition of a book. To assist this we could declare a structure as follows:

struct BOOK

{

char title[50];

char author[50];

char publisher[50];

int price;

};

It is quite clear that a new data type has been created that is BOOK. This is actually the variable type. Struct is a keyword, it indicates a structure. The keyword struct defines BOOK. The elements that are title, author, publisher and price are defined within curly brackets.

The syntax of structure also contains a semi colon after the closing bracket. The elements or the variables inside a structure can be of any type but we could not declare any variable or element of a data type similar to the structure in which it is defined. The variables enclosed in the braces can also be defined as the members of the structure BOOK.

Now in the program in which this structure will be used, each new variable of the type BOOK will have these elements that are title, author, publisher and price. The new variables of type BOOK are similar to declare as simple variables of different data types. Consider the following example:

BOOK novel;

This now can store all the information about the novel.

 

Initialize a structure

To get data into the members of struct, we need to define the initial values in the declaration. Let us suppose that we want to initialize the variable novel to contain the data of a story book.

struct BOOK novel;

now all the information like title of the novel, publisher etc. can be accessed. This is illustrated in the next topic.

 

Accessing the members of a structure

In arrays we can access each element or the member by using its subscript, but there is a different way to access the members of structure. This can be done by using “membership operator”, this is a dot (.).

This is demonstrated in the following example:

#include<iostream>

Using namespace std;

#include <string.h>

#include <conio.h>accessing-the-members-of-a-structure

struct BOOK

{

char title[50];

char author[50];

char publisher[50];

int price;

};

void main()

{

struct BOOK novel;

strcpy ( novel.title, “Robinson Crusoe”);

strcpy (novel.author, “Daniel Defoe”);

strcpy (novel.publisher, “W. Taylor”);

novel.price = 150;

cout<<”Title of book”<<novel.title<<endl;

cout<<”Author of book”<<novel.author<<endl;

cout<<”Publisher of novel”<<novel.publisher<<endl;

cout<<”price of the book is”<<novel.price<<endl;

getch();

}

In the above program strcpy is a string function used to copy the contents of string type in another variable of string or character type.

To use this function a header file is included that is <string.h>. In this way the string like “Robinson Crusoe” is stored in the variable title, the member of the structure BOOK by using the membership operator.

The structure variables can be declared in one statement at the time of the declaration of the structure as follows:

struct BOOK

{

char title[50];

char author[50];

char publisher[50];

int price;

} novel;

It has the same effect as generated by two separated statements.

The data can be entered into structures using strcpy function; it just copies the string to the character type variable. We can also use gets() to enter data into structure. For example:

cout<<”enter title of book”;

gets(novel.title);

when this statement runs it asks the user to enter the title of the book, the gets() statement takes the string and store it into the member of the BOOK type variable novel.

 

Nested Structures

A structure inside another is the nested structure, it means that a structure can contain other structures as well. This is the most convenient way of making complex database management systems. The nested structures are declared as:

struct BOOK

{

char title[50];

char author[50];

char publisher[50];

int price;

};

struct novels

{

struct BOOK novel1;

struct BOOK novel2;

};

In above example the outside structure is simple, the inside structure that is novel further links the newly created member “novel1” to the struct BOOK.

 

nested-structures

 

Structures to Functions

Simple variables can be passed to functions as parameters, likewise structures can also be passed as function parameters. Consider the following example to understand this feature:

#include<iostream>

Using namespace std;

struct BOOK

{

char title[50];

int price;

};

void display(BOOK);

void main ()

{structures-to-functions

BOOK b;

cout<<”enter title of book”;

gets(b.title);

cout<<”enter price of book”;

cin>>b.price;

display(b);

}

void display(BOOK b1)

{

cout<<”Title of book is: “<<b1.title;

cout<<”price of book is: “<<b1.price;

}

Working of the above program:

In the above program a structure namely BOOK has been created and passed to a function as parameter. In the main function data is entered and is stored in a struct type variable b.

This variable is passed as variable of struct BOOK to the function void display(BOOK). In this function the entered data is printed.