OBJECTIVES: CS200 STRUCTURES Manju Muralidharan Priya By the end of this class you will have understood: 1. Definition of a structure 2. Nested Structures 3. Arrays of structure 4. User defined data types 5. Pointers to structures 6. Using structures in functions INTRODUCTION A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name for convenient handling. Structures help to organize data, particularly in large programs, because they allow a group of related variables to be treated as a single unit. E.g. Details of an employee in an organization (i.e. name, address, designation, salary etc.) Arrays cannot be used in this context as they are all different data types (i.e. char, float etc.). The simplest solution to this problem is structures. STRUCTURE DECLARATION Let us create a structure for the above example. char *address; char *designation; float salary; } emp1, emp2; 1
Breakdown 1. The keyword struct is used to declare a structure which is a list of declarations enclosed in braces. 2. Employee is called the structure tag, which is a name given to the structure. 3. The variables that have been declared within the structure definition are called its member variables. 4. The structure declaration as a whole is considered as a data type and emp1 and emp2 are considered as the variables of the structure and a memory space is allocated for them. The amount of memory allocated is the total value of individual members. 5. A structure for which no variable is declared only defines a template and no memory space is set aside for it. 6. Variables for a structure can also be defined later within the program as follows: struct employee emp3; 7. A member of the structure is always referred to and accessed with its structure name. emp3.salary The. Operator is used to connect the name of the structure variable and the member name. printf ( The salary of emp3 is %d, emp3.name); NESTED STRUCTURES Nested structures are structures within structures. Taking the same example as above let us break down the address into a more complex structure, struct employee_address { }; int no; char *street; char *area; int pincode; 2
Including this in place of address in the previous structure struct employee_address address; char *designation; float salary; } emp1, emp2; The above two definitions can also be consolidated by including the definition of address within the definition of employee as follows: strut employee_address { int no; } address; char *street; char *area; int pincode; char *designation; float salary; } emp1, emp2; ARRAYS OF STRUCTURE Let us consider the same example and assume that there are five employees in the organization whose records have to be maintained. The declaration of the structure and the structure variable will be as follows: char *emp_no; 3
} emp[5]; int *salary; The above declaration creates memory space for an array of five employee type. The value to the structure variable can be added as follows: struct employee{ char *emp_no; int *salary; } emp[5] = { { emp1, e001,1000}, { emp2, e002,2000}, { emp3, e003,3000}, { emp4, e004,4000}, { emp5, e005,5000} }; Blank spaces can be left in the assignment where the employee details are unknown. i.e. {, e007,} Employee name and salary are unknown. Any member of the structure can be accessed using the index number along with the structure name. For e.g. printf( The name of the fourth employee is %s, emp[3].name}; USER DEFINED DATA TYPES C allows programmer to define their own datatypes equivalent to the existing system datatypes. For this purpose we use typedef. Let us declare a new datatype date of birth using structures: typedef struct { int dd; 4
int mm; int yyyy;} dob; Given this declaration dob can be used in the structure just like another datatype like int or char or so on. dob emp_dob; POINTER TO STRUCTURES Pointers to structures are just like pointers to any other variable. The declaration is: struct employee{ } *emp; Here emp point to the structure employee and *emp is the pointer. To access the member variables using a structure pointer we use -> (hyphen followed by greater than symbol). emp->name refers to employee name. Any manipulation can be done by through these pointers. STRUCTURES AND FUNCTIONS Structures may be passed to functions either by value or by reference. However, they are usually passed by reference (the address of the structure is passed). Consider the following e.g. struct employee{ }emp1; float basic; float hra; Let us assume that the value of the gross salary is the addition of basic and HRA. Let us write a program which has a function which does this addition operation by using the structure variable and display the gross salary of the employee. 5
#include <stdio.h> }emp1; main() { } char name[20]; float basic; float hra; float gross, grosscalc(); printf( enter employee name ); scanf(%s,emp1.name); printf( enter basic salary ); scanf(%f,emp1.basic); printf( enter hra ); scanf(%f,emp1.hra); gross = grosscalc(&emp1); /* passing address of structure */ printf( the gross salary is : %f, gross); float grosscalc(struct employee *emp) { } return(emp->basic+emp->hra); /* adds basic and hra and returns value to main */ 6