Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Structures. Structure Declaration ä Creates a user-defined data type. ä Gives the compiler a “head’s up” as to what the structure contains.

Similar presentations


Presentation on theme: "Working with Structures. Structure Declaration ä Creates a user-defined data type. ä Gives the compiler a “head’s up” as to what the structure contains."— Presentation transcript:

1 Working with Structures

2 Structure Declaration ä Creates a user-defined data type. ä Gives the compiler a “head’s up” as to what the structure contains. ä Does NOT create an instance of a structure. ä Does NOT allocate any memory.

3 Structure Declaration Example struct pt3d { int pt; double x, y, z; };keyword bracessurroundelementdeclarations tag semicolon required structureelements

4 Structure Definition ä Creates an instance of a structure. ä Allocates memory. ä Associates a variable name with that instance. ä Directly analogous to defining a variable of any of the pre-defined data types.

5 Structure Definition Example int main(void) { int i; struct pt3d pt1, pt2; struct pt3d pt[5];.... return 0; } variabletype variablename(s) arrayvariable

6 Typedef simplifies the syntax ä Almost identical to #define statement. ä Performs text replacement. ä “distributes” any pointer de-reference operators. Syntax: typedef data-type alias; Example: typedef unsigned int size_t; typedef struct pt3d PT3D;

7 Everything to this point typedef struct pt3d PT3D; struct pt3d { int pt; double x, y, z; }; int main(void) { int i; PT3D pt1; PT3D pt[5];.... return 0; } Typedef can appear before structure declaration Structure declaration outside any function to make it have global scope

8 Accessing Elements ä Similar to accessing elements of an array. ä Array: Follow the name of the array with the “structure access operator” (square brackets) surrounding the offset of the element to be accessed. ä Structure: Follow the name of the structure with the “member access operator” (a period) followed by the name of the element to be accessed. ä Use the accessed element just like a plain variable of that same type.

9 Accessing Elements Examples int j; int k[12]; PT3D point; j = 5; k[j] = 42; point.pt = k[j]; j = point.pt + 3;

10 Pointers to Structures ä Work just like pointers to other data types. ä Use the “indirection operator” (asterisk) to “de- reference) the pointer in order to get at the value being pointed to. ä Treat the dereferenced pointer the same as the name of a variable of the type being pointed to.

11 Structure Pointer Examples int j, *m; int k[12]; PT3D point, *p; m = &j; p = &point; *m = 5; // same as j = 5; k[j] = 42; (*p).pt = k[j]; // same as point.pt = k[j]; j = (*p).pt + 3; // same as j = point.pt + 3;

12 Why (*p).pt and not just *p.pt ?  Highest precedence operators: (), [],., -> ä De-reference is second in precedence. ä The code fragment: *p.pt compiles as *(p.pt) ä The operand on the left side of the member access operator must be a variable of structure type.  But p is not a variable of structure type - it points to a variable of structure type. ä So we must override the precedence and force the pointer to be de-referenced first using parentheses.

13 Structure Indirection ä De-referencing structure pointers very common. ä Failing to override operator precedence very common. ä A different operator is available specifically for this purpose. ä If the left operand is a structure to a pointer, then the “structure indirection operator” (a hyphen followed immediately by a greater-than symbol) will first de-reference the pointer and then access the indicated element.  The code : p->pt compiles as (*p).pt

14 Primitive and Utility Functions ä Structure elements frequently change. ä Sloppy use of structures becomes a code maintenance nightmare. ä Primitive and Utility functions provide a localized interface to a structure’s elements. ä ONLY the primitive function should ever directly access the structure elements. ä ONLY the utility functions should ever call the primitive functions. ä The programmer should only use utility (and higher) functions.

15 Get()/Set() Primitive Functions ä One Get()/Set() Pair for each structure element. ä Get() functions read the current value. ä Set() functions write a new value. ä Both return the final value. ä Work best if structures passed by reference.

16 Get()/Set() list for PT3D int GetPT3D_pt(PT3D *p); int SetPT3D_pt(PT3D *p, int v); double GetPT3D_x(PT3D *p); double SetPT3D_x(PT3D *p, double v); double GetPT3D_y(PT3D *p); double SetPT3D_y(PT3D *p, double v); double GetPT3D_z(PT3D *p); double SetPT3D_z(PT3D *p, double v);

17 Get()/Set() Primitive Functions int GetPT3D_pt(PT3D *p) { if (NULL == p) return -1; return p->pt; } int SetPT3D_pt(PT3D *p, int v) { if (NULL == p) return -1; p->pt = v; return GetPT3D_pt(p); } Naming convention: Get[STRUCT]_[element] element type Set() calls the Get() to return value element name NULLcheck

18 Utility Function - set all the point coordinates at once. PT3D NewPoint(int pt, double x, double y, double z) { PT3D temp; SetPT3D_pt(&temp, pt); SetPT3D_x(&temp, x); SetPT3D_y(&temp, y); SetPT3D_z(&temp, z); return temp; }

19 Utility Function - subtract two points (vector between points). PT3D SubtractPT3D(PT3D ptA, PT3D ptB) { PT3D temp; SetPT3D_pt(&temp, 0); SetPT3D_x(&temp, GetPT3D_x(&ptA) - GetPT3D_x(&ptB) ); SetPT3D_y(&temp, GetPT3D_y(&ptA) - GetPT3D_y(&ptB)); SetPT3D_z(&temp, GetPT3D_z(&ptA) - GetPT3D_z(&ptB)); return temp; }

20 Utility Function - Distance between two points double DistanceBetweenPT3D(PT3D ptA, PT3D ptB) { double distance; distance = LengthPT3D(SubtractPT3D(ptA, ptB)); return distance; }

21 Utility Function - Distance between two points double LengthPT3D(PT3D ptA) { double length; int i; for (i = 0, length = 0.0; i < 3; i++) length += pow(GetPT3D_dimensionN(&ptA, i),2); return sqrt(length); }

22 Utility Function - Get coordinate of a point based on index double GetPT3D_dimensionN(PT3D *p, int i) { if (NULL == p) return -1; switch (i) { case 0: return GetPT3D_x(p); break; case 1: return GetPT3D_y(p); break; case 2: return GetPT3D_z(p); break; } return -2; }

23 Simple main() #define PTS (5) int main(void) { int i; PT3D pt[PTS+1]; double theta; for (i = 0; i <= PTS; i++) { theta = i * (2.0*PI/(double) PTS); pt[i] = NewPoint(i, cos(theta), sin(theta), 0.0); fprintPT3D(stdout, pt[i]); } return 0; }

24 Utility function to print structure to a file. void fprintPT3D(FILE *fp, PT3D pt) { int i; fprintf(fp, “%i”, GetPT3D_pt(&pt)); for (i = 0; i <= 3; i++) fprintf(fp, “, %f”, GetPT3D_dimensionN(&pt, i)); fprintf(fp, “\n”); }


Download ppt "Working with Structures. Structure Declaration ä Creates a user-defined data type. ä Gives the compiler a “head’s up” as to what the structure contains."

Similar presentations


Ads by Google