Bài giảng Lập trình C - Session 11: Advanced Data types
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
Explain how structure pointers can be passed as arguments to functions
Explain the typedef keyword
Advanced Data types Session 11 Elementary Programming with C/Session 11/ * of 23 Objectives 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 Explain how structure pointers can be passed as arguments to functions Explain the typedef keyword Elementary Programming with C/Session 11/ * of 23 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 Elementary Programming with C/Session 11/ * of 23 Defining a Structure 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; }; Elementary Programming with C/Session 11/ * of 23 Declaring Structure Variables Once the structure has been defined, one or more variables of that type can be declared Two ways of declaring struct variables. Right after the close bracket of struct declaration. Separated from the struct declaration. Elementary Programming with C/Session 11/ * of 23 Elementary Programming with C/Session 11/ Slide * of 23 struct cat { char bk_name[25]; char author[20]; int edn; float price; } books1, books2; struct cat books1, books2; Or: struct cat books1; struct cat books2; struct cat { char bk_name[25]; char author[20]; int edn; float price; }; Declaring Structure Variables (cont.) Elementary Programming with C/Session 11/ * of 23 Accessing Structure Elements Structure elements are referenced through the use of the dot operator (.), also known as the membership operator Syntax: structure_name.element_name Example: scanf(“%s”, books1.bk_name); Elementary Programming with C/Session 11/ * of 23 Initializing Structures Can be initialized at the time of declaration struct employee { int no; char name [20]; }; Variables emp1 and emp2 of the type employee can be declared and initialized as: struct employee emp1 = {346, “Abraham”}; struct employee emp2 = {347, “John”}; Elementary Programming with C/Session 11/ * of 23 Assignment Statements Used with Structures-1 Possible to assign the values of one structure variable to another variable of the same type using a simple assignment statement If var1 and var2 are structure variables of the same type, the following statement is valid var2= var1; Elementary Programming with C/Session 11/ * of 23 Assignment Statements Used with Structures - 2 In cases where direct assignment is not possible, the built-in function memcpy() can be used Syntax: memcpy (char * destn, char &source, int nbytes); Example: memcpy (&books2, &books1, sizeof(struct cat)); Elementary Programming with C/Session 11/ * of 23 Structures within Structures Possible to have one structure within another structure. A structure cannot be nested within itself struct issue { char borrower [20]; char dt_of_issue[8]; struct cat books; }issl; The way to access the elements of this structure is similar to the one applied for normal structures, issl.borrower To access elements of the structure cat, a part of structure issue, issl.books.author Elementary Programming with C/Session 11/ * of 23 Passing Structures as Arguments Parameters of a function can be structures. This facility is used to pass groups of logically related data items together instead of passing them one by one The type of the argument should match the type of the parameter Elementary Programming with C/Session 11/ * of 23 Array of Structures A common use of structures is in arrays of structures A structure is first defined, and then an array variable of that type is declared Example: struct cat books[50]; To the access the variable named author of the fourth element of the array books: books[4].author Elementary Programming with C/Session 11/ * of 23 Initialization of Structure Arrays Structure arrays are initialized by enclosing the list of values of its elements within a pair of braces Example: struct unit { char ch; int i; }; struct unit series [3] = { {‘a’, 100} {‘b’, 200} {‘c’, 300} }; Example of structure arrays #include struct strucintcal { char name[20]; int numb; float amt; }; void intcal(struct strucintcal); void main() { struct strucintcal customers[50]; int i; clrscr(); /* Accepts data into the structure */ for (i=0; i operator is used to access the elements of a structure using a pointer Example: struct cat *ptr_bk; ptr_bk = &books; printf(“%s”, ptr_bk->author); Structure pointers passed as arguments to functions enable the functions to modify the structure elements directly Example of Pointers to Structures #include struct strucintcal { char name[20]; int numb; float amt; }; void intcal(struct strucintcal); void main() { struct strucintcal *ptr_customers; int i, n; printf("\nEnter the number of customers: "); scanf(“%d”,&n); ptr_customers=(struct structintcal *) malloc(n *sizeof(struct strucintcal)); clrscr(); /* Accepts data into the structure */ for (i=0; iname); Elementary Programming with C/Session 11/ Slide * of 23 printf("\nEnter Customer number: "); scanf("%d", ptr_customers+i)->numb); printf("\nEnter Principal amount: "); scanf("%f", ptr_customers+i)-> amt); intcal(*(ptr_customers+i)); } getch(); } void intcal(struct strucintcal){} //see slide 16 Elementary Programming with C/Session 11/ Slide * of 23 Example of Pointers to Structures Elementary Programming with C/Session 11/ * of 23 The typedef keyword A new data type name can be defined by using the keyword typedef Does not create a new data type, but defines a new name for an existing type Syntax: typedef type name; typedef cannot be used with storage classes #include typedef float deci; typedef deci point; void main() { float num1; deci num2; point sum; printf("\nEnter the values of num1 and num2: "); scanf(“%f%f”,&num1, &num2); sum= num1 + num2; printf("\n The summary of num1 and num2 is: %f“, sum); getch(); } Elementary Programming with C/Session 11/ Slide * of 23 Example of typedef
File đính kèm:
- Bài giảng Lập trình C - Session 11 Advanced Data types.ppt