240 likes | 257 Views
Learn about C structures, creating instances, accessing members, using typedef, arrays of structures, functions with structures, and best practices for variable naming.
E N D
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
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)
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
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;
Creating instances of a structure • Instances can also be created within the structure definition: struct rectangle { double length; double width; } r1, r2;
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
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;
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);
Structures as structure members typedef struct { double x_coord; double y_coord; } point; typedef struct { point p1; point p2; } segment;
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;
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; }
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
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
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]
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
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
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
Functions that accept structure arguments • The following function accepts an instance of the rectangle structure double calculateArea (rectangle r) { return r.width * r.length; }
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; }
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; }
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); • }
Unions • Unions are similar to structures except memory allocation is handled differently • Members occupy the same memory location
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)