19 Structures.pptx - Topic 19 Structures 1 Arrays versus Structures An array is a collection of elements of the same data type Example An array with the

19 Structures.pptx - Topic 19 Structures 1 Arrays versus...

This preview shows page 1 - 15 out of 49 pages.

Topic 19 Structures 1
Image of page 1
Arrays versus Structures An array is a collection of elements of the same data type . Example: An array with the name numbers with 10 elements to store integer values: numbers all elements of type int 2
Image of page 2
A structure is a collection of elements of possibly different data types . Example: A structure with 3 elements of different types: Arrays versus Structures code price quantity product type int type int type double 3
Image of page 3
Structures Each element in a structure is called a member . All the members in a structure are related. Example: the members code, price, and quantity are all related to product. code price quantity product 4
Image of page 4
Declaring Structures A structure type must be declared before it can be used. There are 2 ways to declare a structure type: Using keyword struct Using keywords typedef struct 5
Image of page 5
Using keyword struct General format for using keyword struct: struct TAG { field list } ; Example: struct PRODUCT { int code; double price; int quantity; } ; tag Note: semicolon at the end. keywor d struct 6
Image of page 6
Using keywords typedef struct General format for using keywords typedef struct: typedef struct { field list } TYPE ; Example: typedef struct { int code; double price; int quantity; } PRODUCT_TYPE ; keywords typedef and struct Note: semicolon at end. type nam e 7
Image of page 7
Type Definition A type definition , typedef , creates a new type by giving a name to a data type. The new type can be used anywhere a data type is used. Example 1 : give name INTEGER for data type int: typedef int INTEGER ; 8
Image of page 8
Type Definition This new type can be used as follows: INTEGER sum; // instead of - int sum; Note: by convention, the typedef identifier is in uppercase. Example2 : give name PRODUCT_TYPE for data type struct PRODUCT : typedef struct PRODUCT PRODUCT_TYPE ; 9
Image of page 9
Declaring Structures The structure type declaration is normally put at the top of the program after the preprocessor directives. This makes the declaration a global declaration. It is visible or can be used in all functions of the program. 10
Image of page 10
Declaring Structures #include . . . #define . . . struct . . . { . . . } ; // function prototypes . . . #include . . . #define . . . typdef struct { . . . } . . . ; // function prototypes . . . int main(void) { . . . } Global area int main(void) { . . . } 11
Image of page 11
Declaring Variables of Structure Type Once the structure type is declared, we can declare variables of the structure type. struct PRODUCT { int code; double price; int quantity; } ; int main(void) { struct PRODUCT p; . . . typedef struct { int code; double price; int quantity; } PRODUCT_TYPE ; int main(void) { PRODUCT_TYPE p; . . . 12
Image of page 12
Accessing Members of Structures Each member in a structure can be accessed by using the variable identifier and member name separated by a dot ( selection operator ): int main(void) { struct PRODUCT p; p.code = 997; p.price = 23.50; p.quantity = 47; int main(void) { PRODUCT_TYPE p; p.code = 997; p.price = 23.50; p.quantity = 47; 13
Image of page 13
Accessing Members of Structures PRODUCT_TYPE product; product.code = 997; product.price = 23.50; product.quantity = 47; 997 code 23.50 price quantity produc t 47 14
Image of page 14
Image of page 15

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture