1 / 24

CSCI 171

CSCI 171. Presentation 13 Basic Structures. Structures. Structure – collection of one or more variables grouped under a single name Structures may contain any of C’s simple data types, arrays, or other structures Each variable within a structure is a member of the structure.

Télécharger la présentation

CSCI 171

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. CSCI 171 Presentation 13 Basic Structures

  2. Structures • Structure – collection of one or more variables grouped under a single name • Structures may contain any of C’s simple data types, arrays, or other structures • Each variable within a structure is a member of the structure

  3. Example of a Structure • Assume we need to do calculations with a rectangle figure – we need to keep track of the length and width • Length and width is minimal information • We might need area, but can calculate it • We could create a rectangle structure that has the following members: • width (double) • length (double)

  4. Creating a Structure - Syntax struct { double length; double width; } r1, r2; • A structure has been declared, and 2 instances have been created • No more instances can be created without another declaration

  5. Creating a Structure - Syntax struct rectangle{ double length; double width; } • A structure has been declared (no instances created) • Instances can be created without another declaration • struct rectangle r1, r2;

  6. Creating instances of a structure • Instances can also be created within the structure definition: struct rectangle { double length; double width; } r1, r2;

  7. Creating instances of a structure • Structures can also be created using typedef: typedef struct { double length; double width; } rectangle; • rectangle is the name of the structure type, NOT the instance • Instances can be created via a standard declaration • rectangle r1, r2; • Using typedef is the standard way to create structures

  8. Accessing Structure Members • The structure member operator (also known as the dot member operator) is the period (.) • We can access the member using this operator: r1.width = 3.2; r1.length = 5.7; r2.width = 14.632; r2.length = 8;

  9. Accessing Structure Members • Structure members can be used anywhere the corresponding data type is allowed: • printf(“%lf”, r1.width); • area = r1.width * r1.length; • x = sqrt(r2.length);

  10. Structures as structure members typedef struct { double x_coord; double y_coord; } point; typedef struct { point p1; point p2; } segment;

  11. Accessing members Segment s; s.p1.x_coord = 3.1; s.p1.y_coord = 4.2; s.p2.x_coord = 5.3; s.p2.y_coord = 6.4;

  12. Arrays as structure members • The following code initializes a double array member of a structure to zeros: typedef struct { int id; double exam_grades[3]; double hw_grades[8]; } student; student s; … for (i = 0; i < 8; i++) { s.hw_grades[i] = 0.0; }

  13. Arrays of Structures • Single name identifies the array • Indexed by using subscript within brackets • Each element is an instance of a structure • each element has all the associated members

  14. Arrays of Structures typedef struct { double length; double width; } rectangle; rectangle r[100]; • r is an array capable of holding up to 100 instances of the rectangle structure

  15. Accessing data from Arrays of Structures • From our previous slide: • r[6] is an instance of the rectangle structure • r[6] refers to the 7th element of r • r[6].width refers to the ‘width’ member of r[6] • r[6].length refers to the ‘length’ member of r[6]

  16. Arrays of Structures typedef struct { int id; double exam_grades[3]; double hw_grades[8]; } student; student s[100]; • s is an array capable of holding up to 100 instances of the student structure

  17. Accessing data from Arrays of Structures • From our previous slide: • s[6] is an instance of the student structure • s[6] refers to the 7th element of s • s[6].hw_grades refers to the “hw_grades” member of s[6] • this is an array • s[6].hw_grades[3] refers to the 4th element of the “hw_grades” member of the 7th element of s

  18. Functions and structures • Functions can: • Accept structures as arguments • Return structures • Accept and return structures • The function must ‘know’ about the structure definition • Defined in the same file • Defined in a .h (header file) and included where needed

  19. Functions that accept structure arguments • The following function accepts an instance of the rectangle structure double calculateArea (rectangle r) { return r.width * r.length; }

  20. Functions that return structure arguments • The following function returns an instance of the rectangle structure rectangle createStructure() { rectangle r; printf(“Enter width: “); scanf(“%lf”, &r.width); printf(“Enter length: “); scanf(“%lf”, &r.length); return r; }

  21. Functions that accept and return structures • The following function accepts two structure arguments and returns an instance of the rectangle structure rectangle findAverageRectangle(rectangle r1, rectangle r2) { rectangle r; r.width = (r1.width + r2.width) / 2; r.length = (r1.length + r2.length) / 2; return r; }

  22. Choose variable names wisely • #include <stdlib.h> • void main() { • typedef struct { • int y; • char x; • } aStructure; • aStructure x, y; • x.y = 3; • x.x = 'a'; • y = x; • printf("%d", y.y); • printf("\n%c", y.x); • }

  23. Unions • Unions are similar to structures except memory allocation is handled differently • Members occupy the same memory location

  24. Sample code for a union • typedef union { • int x; • double y; • } myUnion; • myUnion z; • z.x = 2; • z.y = 3.7; • printf("%lf", z.y); //Output is 3.7 • printf(“%d”, z.x); //Bad output!!! (it won’t be 2)

More Related