Session 11

| Advanced Data types and Sorting Session 11 Objectives - 1 Explain structures and their use Define structures Declare structure variables Explain how structure elements are accessed Explain how structures are initialized Explain how assignment statements are used with structures Explain how structures can be passed as arguments to functions Use arrays of structures Explain the initialization of structure arrays Objectives - 2 Explain pointers to structures Explain how structure pointers can be passed as arguments to functions Explain the typedef keyword Explain array sorting with the Selection sort and Bubble sort methods Structures A structure consists of a number of data items, which need not be of the same data type, grouped together The structure could hold as many of these items as desired 1 Variable I L L U S I O N Array I L L U S I O N B A C H 1 } } Name of the book Author Edition Structure Defining a Structure A structure definition forms a template for creating structure variables The variables in the structure are called structure elements or structure members Example: struct cat { char bk_name [25]; char author [20]; int edn; float price; }; Declaring Structure Variables Once the structure has been defined, one or more variables of that type can be declared Example: struct cat books1; The statement sets aside enough memory to hold all items in the structure struct cat { char bk_name[25]; char author[20]; int edn; float price; } books1, books2; Other ways struct cat books1, books2; or struct cat books1; struct cat books2; Accessing Structure Elements Structure elements are referenced through the use of the dot operator (.), also known as the membership operator Syntax: Example: scanf(“%s”, ); Initializing Structures Like variables and arrays, structure variables can be initialized at the point of declaration struct employee { int no; char name [20]; }; Variables emp1 and emp2 of the type employee can be declared .

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG