Vous êtes sur la page 1sur 4

Structures (i)

A structure is a collection of variables referenced under one name; it provides a convenient way of storing information. For example, the data that we might want to store about a student: Student ID Name Address Phone number Age Sex This data can be grouped into a structure as follows: struct student{ char id[10]; char name[50]; char address[50]; char phone[7]; char sex; int age; }; 1

Structures (ii)
A structure definition creates a format that may be used to declare structure variables. The variables that make up a structure are called its members. The members of a structure are usually logically related Once a structure is created, we can have variables of type of the structure we have defined. E.g. we can have a variable of type student student paul;
Variable name

Name of structure

Once a structure is created, it needs to be accessed. For example, to access: name: paul.name; Age paul.age; To summarize, its: nameOfVariable.member

Structures (iii)
We can define a structure to hold information as follows: struct book_bank { char title[20]; char author[15]; int pages; float price; }; The keyword struct declares a structure that hold the details of four fields, namely title, author, pages and price. These fields are called structure elements or members. Each member may belong to a different type of data. book_bank is the name of the structure and is called the structure tag. The tag name may be used subsequently to declare variables that have the tags structure. Note that the above declaration has not declared any variables.
3

Structures (iv)
We can declare structure variables using the tag name anywhere in the program. For example, the statement struct book_bank book1, book2, book3; declares book1, book2 and book3 as variables of type struct book_bank. The complete declaration might look like this: struct book_bank { char title[20]; char author[15]; int pages; float price; }; struct book_bank book1, book2, book3;
4

Structures (v)
It is also allowed to combine both the template declaration and variables declaration in one statement as follows: struct book_bank { char title[20]; char author[15]; int pages; float price; } book1, book2, book3; Normally, structure definitions appear at the beginning of the program file, brfore any variables or functions are defined. They may also appear before the main.

Reading/Writing
We can assign values to the members of a structure in a number of ways. The member (eg: pages, price) themselves are not variables. They should be linked to the structure variables (eg: book1, book2.) inorder to make them meaningful members. The link between a member and a variable is established using the member operator . which is also known as dot operator or period operator. For example, book1.price is the variable representing the price of book1 and can be treated like any other ordinary variable. Once we have declared a variable of type of a structure previously described; We can use it to input data as follows: cout << Enter name: ; cin.getline(paul.name,50,\n); cout << Enter age: ; cin >> paul.age; We can use it as a source of output too: cout << Name: << paul.name; 6 cout << Age: << paul.age;

Reading/Writing
Define a structure type, struct perosnal, that would contain person name, date of joining and salary. Using this structure write a program to read that information for one person from the keyboard and print the same on the screen. #include<iostream.h> #include<conio.h> struct personal { char name[20]; int day; char month[10]; int year; float salary; };

Reading/Writing
void main() { struct personal person; clrscr(); cout<<"Input values: \n"; cout<<"Name: \t"; cin>>person.name; cout<<"Day: \t"; cin>>person.day; cout<<"Month: \t"; cin>>person.month; cout<<"Year: \t"; cin>>person.year; cout<<"Salary: \t"; cin>>person.salary;

Reading/Writing
//Output values cout<<"\n\n\n"; cout<<person.name<<"\t"<<person.day<<"\t"; cout<<person.month<<"\t"<<person.year<<"\t"; cout<<person.salary; getch(); } Exercise 2: Write a program that will enter data of the client of a bank in the structure bankAccount previously defined and display the data entered.

Structures within a structure


Structures within a structure means nesting of structures. Consider the following structure defined to store information about the date. struct date{ int day; int month; int year; }; Consider the following structure person defined to store a persons detail like name, sex and date for (date of birth). Date is the substructure containing members day, month and year. struct person{ char name[25]; date dob; char sex; }; struct person person1;
10

Structures within a structure


The inner-most member in a nested structure can be accessed by chaining all the conserned structure variables( from outer-most to inner-most) with the member using dot operator. For example, person1.date.month; person1.date.day; Exercise 1: Define a structure bankAccount that stores: account number, name of holder, address of holder, current balance

Array of structures
Array of structures Once a structure has been defined, we can create an array of structures to describe the format of a number of related variables. For example , in analyzing the marks obtained by a class of students, we may use a template to describe student names and marks obtained in various subjects and then declare all the students as structure variables. We may declare an array of structures, each element of the array representing a structure variable Consider a structure class with member as number, name and marks. struct class{ int number; char name[20]; float marks; };

11

12

Array of structures
You can declare array of structure student as follows: struct class student[100]; This defines an array called student, that consists of 100 elements. Each element is defined to be of the type struct class To refer to the name of the student in a class: student[0].name; Exercise 3: How would you refer to the address of the 25th student in class? Write a segment of code that will allow the data for a whole class of 30 students to be entered into the array class. (Hint: you should use loops). Note: You can have multidimensional array of structures
13

Structures and functions


Structures can be passed to functions as well. They can be passed either by value or reference as with normal variables. You can pass the whole structure or individual members The general format of sending a copy of structure to the called function is: function name(structure variable name) The called function takes the following form: Data_type function name(structure_name) Structure_type structure_name { .. return(expression); }

14

Structures and functions


E.g. Passing a whole structure by value and reference respectively: void display(student x); void input(student &x); Passing individual members: void display(int y); display(paul.age); Exercise 4: Rewrite the program in exercise 2 but this time, use functions: a function to input data and another to display the data entered. You program should allow you to specify the number of clients for which you need to perform data entry (to a maximum of 50) and display the clients entered.

Summary
In this lecture, we have looked at: Declaring structures Accessing structures Array of structures Passing structures to functions

15

16