130 likes | 251 Views
ETE 132 Lecture 7. By Munirul Haque. Topics. Defining Your Own Types Structures. Structures. The structure mechanism allows us to aggregate variables of different types
E N D
ETE 132Lecture 7 By Munirul Haque
Topics • Defining Your Own Types • Structures
Structures • The structure mechanism allows us to aggregate variables of different types • struct definitions are usually placed outside of functions so that they are in scope throughout the file, as in the following example:
Structures struct card_struct { int number; char suit; }; /* note the semicolon after the definition! */ void some_function() { struct card_struct a, b; a.number = 3; a.suit = ’D’; b = a; }
Structures • The “.” in a.num is the “structure member operator”, which connects the structure name and the member name. • A “member” is a variable in a structure. • Assignment (=) works just as you would expect, as if there were a separate assignment for each structure member.
More on structures The reason a semicolon follows the struct definition is that the definition is a statement. • Also, you can declare a variable of that struct type using basically the same syntax: struct card_struct { int number; char suit; } my_card;
Example: points in the plane void main() { /* here’s a convenient notation for structure initialization: */ struct point a, b; a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5; printf( "Distance: %f\n", distance( a, b )); } #include <math.h> struct point { double x; double y; }; double distance( struct point p1, struct point p2 ) { double dx, dy, dist; dx = p1.x - p2.x; dy = p1.y - p2.y; dist = sqrt((dx * dx) + (dy * dy)); return( dist ); }
What happens here? struct employee { char name[50]; int age; }; void main() { struct employee tom1, tom2; strcpy( tom1.name, "Thomas Wolfe" ); tom1.age = 104; tom2 = tom1; tom2.name[0] = ’G’; printf( "Name: %s", tom1.name ); }
A Comparison Function • There’s no standard way to compare structures. You can’t try tom1 == tom2 in the previous example, or tom1 < tom2. • You can always write comparison code, if you need to: int compare_employees( struct employee e1, struct employee e2 ) { return (e1.age == e2.age) && (strcmp(e1.name,e2.name) == 0); }
Structure member as Parameter void sum(double p1_x,double p1_y,double p2_x,double p2_y ) { struct point psum; psum.x = p1_x + p2_x; psum.y = p1_y + p2_y; printf(“%lf_%lf\n”, psum.x, psum.y); } void main() { struct point a, b, c; a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5; printf(“%lf_%lf_%lf_%lf\n”, a.x, a.y, b.x, b.y); sum( a.x, a.y, b.x, b.y); } 10_5 3.5_4.5_6.5_0.5
Structure as Parameter • Structures work seamlessly with functions. • A structure is a type, so it can be the type of a function parameter (as here), or a return type: point sum( struct point p1, struct point p2 ) { struct point psum; psum.x = p1.x + p2.x; psum.y = p1.y + p2.y; return psum; } void main() { struct point a, b, c; a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5; c = sum( a, b); printf(“%lf_%lf_%lf_%lf_%lf_%lf\n”, a.x, a.y, b.x, b.y, c.x, c.y); } 3.5_4.5_6.5_0.5_10_5
Array of Structure You can also define an array of structures: struct point point_list[100]; for(i=0;i<100;i++) { /* initialize point i */ point_list[i].x = (i+1)*2; point_list[i].y = (i+1)*3; } for(i=0;i<100;i++) { printf(“Point %d: %lf_%lf\n”, (i+1), point_list[i].x, point_list[i].y); } Point 1: 2_3 Point 2: 4_6 Point 3: 6_9 … … … Point 100: 200_300
Problem • Write a program to take input from console, Name (within 20 characters), Address (50 char), TelephoneNo (max. 9 digit), and Gender (single char) of 15Students. • Print those students information in reverse order (started with Gender, ended with Name for every student)