1 / 30

Engineering Problem Solving with C Fundamental Concepts

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 .

aricin
Download Presentation

Engineering Problem Solving with C Fundamental Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures

  2. Structure Basics • A structure is a collection of data values, called datamembers, 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 membersare 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.

  3. Structure Definition structname { variable declaration; variable declaration; . . }; the keyword struct announces that this is a structure definition

  4. Continue.. • struct hurricane structure name/structure tag { char name[10]; int year, category; }; • 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 Data members

  5. name h1 ? year ? category ? 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 struct hurricane h1;

  6. Example struct node { int data; node *link; }; • the above defines a structure type • the name of the structure type (node) is called the structuretag • the identifiers declared inside the braces are called membernames. 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

  7. Declaring Structure Variables struct node n1; struct node *head; The structure n1 is a collection of smaller variables called membervariables. The member variables are accessed using the dot (.) operator; head =&n1; n1.data = 2; n1.link = NULL; 2

  8. name h1 “Camille” year 1969 category 5 Initializing Structures • Using a declaration statements: struct hurricane h1= {“Camille”,1969,5}; The declaration statement allocates memory for the three data members, with their initial values.

  9. Another way to initialize.. • Using program statements: h1.name = “Camille”; h1.year = 1969; h1.category = 5;

  10. Initializing Structures struct Rect { double x; double y; double width; double height; }; struct Rect r1 = {0,0,5,10};

  11. Practice! Write the C statements necessary to output the value of the structure r1 defined in the previous slide.

  12. Input and Output • We can use scanf and fscanfstatements to read values into the data members of a structure. • printfand fprintfto print the values. • The structure member operator must be used to specify an individual data member • Example: • printf(“Hurricane: %s”,h1.name); • printf(“Year: %d, Category: %d”,h1.year,h1.category)

  13. 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 h1; /* Initialization */ //struct hurricane h1 = {"Camille",1969,5}; another way to initialize strcpy(h1.name,"Camille"); //h1.name = "Camille"; h1.year = 1969; h1.category = 5; printf("%s, %d, %d \n", h1.name, h1.year, h1.category); return 0; }

  14. Scope of Structure • Member variables are local to the structure. • Member names are not known outside the structure.

  15. Assignment operator Assignment operator is defined for structure of the sametype. (Compiler looks at tag, not composition.)

  16. Example #include <stdio.h> #include <string.h> struct Car { char model[10], color[10]; double price; int year; }; int main() { struct Car Car1, Car2; Car1.year = 99; Car1.price = 18599.99; strcpy(Car1.color,"red"); strcpy(Car1.model,"Contour"); /* Copy all data from Car1 to Car2. */ Car2 = Car1; /* print the data*/ printf("%d, %lf, %s, %s \n", Car1.year,Car1.price, Car1.color, Car1.model); printf("%d, %lf, %s, %s \n", Car2.year,Car2.price, Car2.color, Car2.model); return 0; }

  17. 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.

  18. Arrays of Structures Struct Car { char model [10]; }; • Arrays of structures may be declared in the same way as other C data types. structCar Ford[20]; • Ford[0] references first structure of Ford array. Ford[0].model = “Taurus”;

  19. Example • struct hurricane { char name[10]; int year, category; }; …… struct hurricane h[25]; h[0] h[1] …… h[24]

  20. 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;

  21. Output • To print the first array • print_hurricane(h[0]); • output: Hurricane: Camille Year: 1969, Category: 5

  22. 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.

  23. 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.

  24. Call by Value Example struct simple { int ival; double dval; }; int main(void) { void fun1(struct simple s); //function prototype struct simple s1 = {10, 1.5}; //structure variable fun1(s1); //call function fun1 printf(“%i %lf”, s1.ival , s1.dval ); return 0; } void fun1(struct simple s) output?___________ { s.ival++; s.dval++; }

  25. To see the difference… #include <stdio.h> struct simple { int ival; double dval; }; int main(void) { void fun1(struct simple s); //function prototype struct simple s1 = {10, 1.5}; //structure variable fun1(s1); //call function fun1 printf("in the main program:\n"); printf("%i %lf\n", s1.ival , s1.dval ); return 0; } void fun1(struct simple s) { s.ival++; s.dval++; printf("within the function:\n"); printf("%i %lf\n",s.ival , s.dval ); }

  26. #include <stdio.h> • struct employee //define structure name employee • { • int idnum; • double payrate; • double hours; • }; • double calcNet(struct employee); //funct prototype • int main() • { • struct employee emp={6782,8.93,40.5}; • double netPay; • netPay=calcNet(emp); //pass copy of the values in variable emp of structure employee • printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay); • return 0; • } • double calcNet(struct employee temp) //funct header • { • return (temp.payrate * temp.hours); • }

  27. 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;

  28. Pointers to Structures struct node { int data; struct node *link; }; int main(void) { struct node *nptr, n={2, NULL}; //declare structure variable void fun1(struct node*); //function prototype nptr = &n; //pointer to structures fun1(nptr); //call function fun1 printf(“%i”, n.data ); return 0; } Output?___________________ void fun1(struct node* n) { n->data++; }

  29. Source Code Structure Definition #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; medic=&engin; (*medic).aa = 20; medic->bb =15.7; printf("%d %lf\n",engin.aa,engin.bb); } 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 Using pointer to access the structure variable’s members. Two methods are shown. Both accomplish the same task. Printing the engin member, using pointer will modify the members of engin

  30. #include <stdio.h> • struct employee //define structure name employee • { • int idnum; • double payrate; • double hours; • }; • double calcNet(struct employee *temp); //funct prototype • int main() • { • struct employee emp={6782,8.93,40.5}; • double netPay; • netPay=calcNet(&emp); //pass address of emp of structure employee • printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay); • return 0; • } • double calcNet(struct employee *temp) //funct header • { • return (temp->payrate * temp->hours); • // or return (((*temp).payrate) * ((*temp).hours)); • }

More Related