f17aaa3857e3d788d22dfc3b88b954f7.ppt
- Количество слайдов: 30
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures
Structure Basics • A structure is a collection of data values, called data members, that form a single unit. • But the individual parts of the data do not have to be the same type. • Structures are often called aggregate data type, because they allow multiple data values to be collected into a single data type. • Data members are individual data values within a structure. • A structure provides a means of grouping variables under a single name for easier handling and identification. • Unlike arrays, the data members can be of different types.
Structure Definition struct name { variable declaration; . . }; the keyword struct announces that this is a structure definition
Continue. . • struct hurricane { structure name/structure tag char name[10]; int year, category; Data members }; • After a structure has been defined, structure variables can be defined using declaration statements. • Members can be declared to be of any valid C data type the tag node may now be used just like predefined types int, char, etc
Declaring Structure Variables • The statements to define structure appear before the main function • They are often stored in the header file. • Define a variable of this structure by the following statement name h 1 ? struct hurricane h 1; year ? ? category
Example struct node { int data; node *link; }; • the above defines a structure type • the name of the structure type (node) is called the structure tag • the identifiers declared inside the braces are called member names. Members can be declared to be of any valid C data type • the tag node may now be used just like predefined types int, char, etc
Declaring Structure Variables struct node n 1; struct node *head; The structure n 1 is a collection of smaller variables called member variables. The member variables are accessed using the dot (. ) operator; head =&n 1; n 1. data = 2; n 1. link = NULL; 2
Initializing Structures • Using a declaration statements: struct hurricane h 1= {“Camille”, 1969, 5}; h 1 “Camille” 1969 5 name year category The declaration statement allocates memory for the three data members, with their initial values.
Another way to initialize. . • Using program statements: h 1. name = “Camille”; h 1. year = 1969; h 1. category = 5;
Initializing Structures struct Rect { double x; double y; double width; double height; }; struct Rect r 1 = {0, 0, 5, 10};
Practice! Write the C statements necessary to output the value of the structure r 1 defined in the previous slide.
Input and Output • We can use scanf and fscanf statements to read values into the data members of a structure. • printf and fprintf to print the values. • The structure member operator must be used to specify an individual data member • Example: – printf(“Hurricane: %s”, h 1. name); – printf(“Year: %d, Category: %d”, h 1. year, h 1. category)
Example: code #include <stdio. h> #include <string. h> /* Define structure to represent a hurricane */ struct hurricane { char name[10]; int year, category; }; int main (void) { /* Declare variables */ struct hurricane h 1; /* Initialization */ //struct hurricane h 1 = {"Camille", 1969, 5}; another way to initialize strcpy(h 1. name, "Camille"); //h 1. name = "Camille"; h 1. year = 1969; h 1. category = 5; printf("%s, %d n", h 1. name, h 1. year, h 1. category); return 0; }
Scope of Structure • Member variables are local to the structure. • Member names are not known outside the structure.
Assignment operator is defined for structure of the same type. (Compiler looks at tag, not composition. )
Example #include <stdio. h> #include <string. h> struct Car { char model[10], color[10]; double price; int year; }; int main() { struct Car 1, Car 2; Car 1. year = 99; Car 1. price = 18599. 99; strcpy(Car 1. color, "red"); strcpy(Car 1. model, "Contour"); /* Copy all data from Car 1 to Car 2. */ Car 2 = Car 1; /* print the data*/ printf("%d, %lf, %s n", Car 1. year, Car 1. price, Car 1. color, Car 1. model); printf("%d, %lf, %s n", Car 2. year, Car 2. price, Car 2. color, Car 2. model); return 0; }
Arrays of Structures • One benefit of having structure type in C is that we can create arrays of structures. • With that, we can solve problems much more quickly and easily.
Arrays of Structures Struct Car { char model [10]; }; • Arrays of structures may be declared in the same way as other C data types. struct Car Ford[20]; • Ford[0] references first structure of Ford array. Ford[0]. model = “Taurus”;
Example • struct hurricane { char name[10]; int year, category; }; …… struct hurricane h[25]; h[0] h[1] …… ? ? ? … … … h[24] ? ? ?
Continue. . • To access an individual data members : specify the array name, a subscript, and the data member name. • Example: h[0]. name = “Camille”; h[0]. year = 1969; h[0]. category = 5;
Output • To print the first array – print_hurricane(h[0]); – output: Hurricane: Camille Year: 1969, Category: 5
Structures as Arguments to Functions • Entire structures can be passed as arguments to functions. • When a structure is passed as an argument to a function, it is a call-by-value reference. When a function referenced is made, each data member of the actual parameter is passed to the function and is used as the value of the corresponding data member of the formal parameters – Changes made to the formal parameters do not change the argument.
Continue • A pointer to a structure may also be passed as an argument to a function. – Changes made to the formal parameters also change the argument.
Call by Value Example struct simple { int ival; double dval; }; int main(void) { void fun 1(struct simple s); //function prototype struct simple s 1 = {10, 1. 5}; //structure variable fun 1(s 1); //call function fun 1 printf(“%i %lf”, s 1. ival , s 1. dval ); return 0; } void fun 1(struct simple s) output? ______ { s. ival++; s. dval++; }
To see the difference… #include <stdio. h> struct simple { int ival; double dval; }; int main(void) { void fun 1(struct simple s); //function prototype struct simple s 1 = {10, 1. 5}; //structure variable fun 1(s 1); //call function fun 1 printf("in the main program: n"); printf("%i %lfn", s 1. ival , s 1. dval ); return 0; } void fun 1(struct simple s) { s. ival++; s. dval++; printf("within the function: n"); printf("%i %lfn", s. ival , s. dval ); }
• • #include <stdio. h> struct employee //define structure name employee { int idnum; double payrate; double hours; }; • • double calc. Net(struct employee); //funct prototype int main() { struct employee emp={6782, 8. 93, 40. 5}; double net. Pay; net. Pay=calc. Net(emp); //pass copy of the values in variable emp of structure employee • • • } • • double calc. Net(struct employee temp) //funct header { return (temp. payrate * temp. hours); } printf("the net pay of the employee %d is $ %6. 2 f", emp. idnum, net. Pay); return 0;
Pointers to Structures When using pointers to access structure members, the arrow operator is used. Example: struct node { int data; struct node *link; }; struct node *nptr, n; nptr = &n; nptr->data = 2; //(*nptr). data nptr->link = NULL;
Pointers to Structures struct node { int data; struct node *link; }; int main(void) { struct node *nptr, n={2, NULL}; void fun 1(struct node*); } nptr = &n; fun 1(nptr); printf(“%i”, n. data ); return 0; void fun 1(struct node* n) { n->data++; } //declare structure variable //function prototype //pointer to structures //call function fun 1 Output? __________
Source Code #include <stdio. h> struct data { int aa; double bb; }; void main(void) { struct data engin; struct data *medic; engin. aa=8; engin. bb=12. 5; Structure Definition Declaring a pointer to structure ( a variable that can store the address of the beginning of a data structure) Assigning an address to the pointer medic=&engin; (*medic). aa = 20; medic->bb =15. 7; Using pointer to access the structure variable’s members. Two methods are shown. Both accomplish the same task. printf("%d %lfn", engin. aa, engin. bb); } Printing the engin member, using pointer will modify the members of engin
• • #include <stdio. h> struct employee //define structure name employee { int idnum; double payrate; double hours; }; • • double calc. Net(struct employee *temp); //funct prototype int main() { struct employee emp={6782, 8. 93, 40. 5}; double net. Pay; net. Pay=calc. Net(&emp); //pass address of emp of structure employee • • • } • • • double calc. Net(struct employee *temp) //funct header { return (temp->payrate * temp->hours); // or return (((*temp). payrate) * ((*temp). hours)); } printf("the net pay of the employee %d is $ %6. 2 f", emp. idnum, net. Pay); return 0;
f17aaa3857e3d788d22dfc3b88b954f7.ppt