Introduction Structure


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:

The keyword “struct” is used to define a structure in C programming language. The keyword ‘struct’ defines a new data type which is defined as the collection of different type of data. The following is the syntax to declare a structure in C programming language:

struct name-of-structure

{

statements;

};


Example of 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.


Declaring Structure Variables:

In C programming language we can declare the variables of the structure after defining the structure. A structure type variable is similar to declare like other variables of built in data types. There are two ways to declare structure type variables in C programming language:


Declaring structure variables separately:

Consider the following example in which we have declared the structure variables separately:

struct BOOK

{

char title[50];

char author[50];

char publisher[50];

int price;

};

struct BOOK b1, b2; //declaration of structure variables


Declaring Structure Variables with structure definition:

The structure variables can be declared when the structure is defined. Consider the following example in which we have declared the structure variables within the definition of the structure:

struct BOOK

{

char title[50];

char author[50];

char publisher[50];

int price;

} b1, b2;

In this example b1 and b2 are the variables of structure BOOK.


Accessing Structure members:

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” that is a dot (.).

This is demonstrated in the following example:

# include <stdio. h>

# include <string. h>

#include <conio.h>

struct BOOKOutput

{

char title [50];

char author [50];

char publisher [50];

int price;

};

void main()

{

struct BOOK novel;

strcpy ( novel. title, “Robinson Crusoe”); // novel is the variable of structure book and title is member of structure book

strcpy (novel. author, “Daniel Defoe”); // novel is the variable of structure book and author is member of structure book

strcpy (novel. publisher, “W. Taylor”); // novel is the variable of structure book and publisher is member of structure book

novel. price = 150;

printf (“Title of book %s\n”, novel. title);

printf (“Author of book %s\n”, novel. author);

printf (“Publisher of novel %s\n”, novel. publisher);

printf (“price of the book is %d\n”, novel. price);

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:

printf (“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.


Structure Initialization:

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. A structure variable can be initialized at the time of compilation, consider the following example:

struct BOOK

{

int pages;

int price;

} novel;

novel = {240, 400}; // initialization of members of structure

The structure variables can also be initialized by using the following method:

struct BOOK b1;

b1. pages = 240 // initialization of members of members of structure separately;

b1. price = 400;


Array of Structure:

An array of structure can be declared in C programming language. Each element of the array will represent the variable of the structure. Consider the following line of code in which an array of structure is declared:

struct student stu [5];

This statement defines an array stu that has 5 elements. The elements of this array are of student (structure) data type.

Consider the following example in which we have an array of structure:

CODE:

# include <stdio. h>

# include <conio. h>

struct student

{

 char sname [10];

 int fee;

};

struct student stu [5];

int x, y;

void get ()

{

 for(x = 0; x < 3; x ++)

 {

  printf (“\nEnter %dst Student record\n”, x + 1);

  printf (“\nStudent name\t”);

  scanf (“%s”, stu [x]. sname);

  printf (“\nEnter Fee of student\t”);

  scanf (“%d”, &stu [x]. fee);

 }

 printf(“\nDisplaying Student record\n”);

 for (x = 0; x < 3; x ++)

 {

  printf (“\nStudent name is %s”, stu [x]. sname);

  printf (“\nFee is %d”, stu [x]. fee);

 }

}

void main ()

{

 clrscr ();

 get ();

 getch ();

}


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.


novel1

Structure as function arguments:

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 <stdio. h>

# include <conio. h>

struct BOOK

{

char title[50];

int price;

};

void display (BOOK);

void main ()

{

BOOK b;150

printf (“Enter title of book”);

gets (b. title);

printf (“Enter price of book”);

scanf (“%d”, &b. price);

display (b);

}

void display (BOOK b1)

{

printf (“Title of book is: %s”, b1. title);

printf (“Price of book is: %d”, 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.