1 / 17

ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2

ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2. Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE. Syllabus. Nested Structures Structures and Functions Unions Examples.

Download Presentation

ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2

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. ECE 103 Engineering ProgrammingChapter 50Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

  2. Syllabus • Nested Structures • Structures and Functions • Unions • Examples

  3. Nested Structures A structure definition may include, among its members, a structure variable of adifferent type. To access a structure within a structure, use the member operator ( . or -> ) multiple times. 2

  4. Example: typedef struct point2D /* Coordinates of a 2-D point */{ double x, y;} Point2D; struct triangle /* Triangle's vertices are points */{ Point2D vertex1, vertex2, vertex3;}; struct triangle tri[3]; /* Array of three triangles */struct triangle * ptr; /* Pointer to a triangle */ /* Initialize vertex1 of second triangle (index 1) */ tri[1].vertex1.x = 0; tri[1].vertex1.y = 0; ptr = &tri[2]; /* Point to third triangle */ptr->vertex1.x = 5; 3

  5. Self-Referential Structures It is illegal to define a structure with a member variable of the same type as the structure. It is perfectly legal to define a structure that contains a pointer to the same type as itself. These are called “self-referential structures” and are used in data structures such as linked lists and trees. 4

  6. Examples: /* This is NOT legal in C */ struct list_node { int value; struct list_node next; /* WRONG */ } /* These are legal in C */ struct list_node { int value; struct list_node * next; /* Uses pointer, so OK */ }; struct tree_node { int value; struct tree_node * left, * right; /* Uses pointers, so OK */ }; 5

  7. Structures and Functions Multiple values can be stored in a structure and passed to a function. This reduces the number of individual arguments that need passing. Passing a structure to a function by value: A copy of the structure variable’s contents is passed to the function. The function can change the copy but not the original argument. If the passed structure contains an array, a complete copy of the array is passed. 6

  8. Passing a structure to a function by reference: Pass by reference can be simulated by passing a pointer to the structure as the function argument. It is more efficient to pass large structures to a function by reference. If the pointed-to structure should not be changed, then use the const modifier. Multiple values can also be returned from a function by storing them in a structureand returning the structure. 7

  9. Example: typedef struct point2D{ double x, y;} Point2D; void fun_v (Point2D p){ p.x = 10.5; p.y = 21.0;} void fun_p (Point2D * p){ p->x = 31.0; p->y = 500.0;} int main (void){ Point2D point; point.x = 1.0; point.y = 5.0; fun_v(point); /* After return from call: 1.0 5.0 */ fun_p(&point); /* After return from call: 31.0 500.0 */ return 0;} 8

  10. Example: #include <stdio.h> #define MAXSIZE 100 typedef struct /* Encapsulate array data in a structure */ { int maxsize; /* Maximum array size */ int numel; /* Current number of elements */ int ele[MAXSIZE]; } array; void DispArray (const array * p) { int k; for (k = 0; k < p->numel; k++) printf("%d\n", p->ele[k]); } int main (void) { array A1, A2; A1.maxsize=MAXSIZE; A1.ele[0]=5; A1.ele[1]=-3; A1.numel=2; A2 = A1; DispArray(&A2); return 0; } 9

  11. Example: Actual Output: Pre : mag = 0.000000 phase = 0.000000 Post: mag = 6.403124 phase = 0.896055 #include <stdio.h> typedef struct /* Use this to hold multiple return values */ { double mag, phase; /* Magnitude & phase of complex number */ } MagPhase; MagPhase calculate_magphase (double a, double b) { MagPhase mp; /* Hold calculated values for return */ mp.mag = sqrt(a*a + b*b); mp.phase = atan2(b,a); return mp; } int main (void) { MagPhase Q = {0, 0}; printf("Pre : mag = %f phase = %f\n", Q.mag, Q.phase); Q = calculate_magphase(4, 5); printf("Post: mag = %f phase = %f\n", Q.mag, Q.phase); return 0; } 10

  12. Unions Unions offer a way to economize on storage. Like a structure, a union is a collection of variables of different types. However, only a single field can actually hold information at any given time. 11

  13. A union definition is similar to a structure definition. Example: /* Define a union */ union UData { char c_number; int i_number; float f_number; double d_number; }; 12

  14. To define a variable of type union UData: union UData num; To assign values to num: num.c_number = 'A'; num.f_number = 3.14; num.i_number = 2; Assigning 3.14 to num cancels the original assignment of 'A' to num. Likewise, storing the integer value 2 in num will cancel the assignment of 3.14. 13

  15. For a union variable, the system allocates an amount of storage large enough to hold its largest member. This same storage is used for all the smaller union members as well. A member of a union can be: A variable or array of any legal data type A structure or another union A union can be used to view a single piece of data as different types, as specified in the union declaration. 14

  16. Example: struct SData { int x; double y; char ch; }; struct SData snum; snum.x = 256; snum.y = -6.73; snum.ch = 'A'; snum x y ch 256 -6.73 'A' 15

  17. Example: union UData { int x; double y; char ch; }; struct UData unum; unum.x = 256; unum.y = -6.73; unum.ch = 'A'; unum y x -6.73 256 ch 'A' 16

More Related