Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.

Similar presentations


Presentation on theme: "Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped."— Presentation transcript:

1 Engineering Computing I Chapter 6 Structures

2 Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.  Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities Spring 20112Chapter 6

3 Examples of Structures payroll record: o address address 1 address 2 city State Zip code o social security number o salary base salary overtime o social security number Spring 20113Chapter 6

4 Basics of Structures declaration structure tag member struct { int x; int y; } x, y, z; struct point pt; Spring 20114Chapter 6

5 Referring to a member of a structure structure-name.member printf("%d,%d", pt.x, pt.y); dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y); Spring 20115Chapter 6

6 Nested Structures Spring 20116Chapter 6

7 Structures and Functions legal operations on a structure  copying it o passing arguments to functions o and returning values from functions  assigning to it as a unit  taking its address with “&”  accessing its members  Structures may not be compared!  A structure may be initialized by a list of constant member values Spring 20117Chapter 6

8 Example A function returns a structure Spring 20118Chapter 6

9 Arrays of Structures Consider writing a program to count the occurrences of each C keyword Spring 20119Chapter 6

10 Pointers to Structures To Be enhanced by simpler examples Spring 201110Chapter 6

11 Typedef C provides a facility called typedef for creating new data type names Spring 201111Chapter 6

12 Typedef Spring 201112Chapter 6

13 Unions A union  is a variable that may hold (at different times) objects of different types and sizes  the compiler keeps track of size and alignment requirements.  Unions provide a way to manipulate different kinds of data in a single area of storage,  without embedding any machine-dependent information in the program Spring 201113Chapter 6

14 Unions Syntactically, members of a union are accessed as union-name.member or union-pointer->member Example /simpler Spring 201114Chapter 6

15 Bit-fields When to Use Bit-fields? storage space is at a premium one common use is a set of single-bit flags in applications like compiler symbol tables hardware devices registers, also often require the ability to get at pieces of a word Spring 201115Chapter 6

16 Bit-fields turns on the EXTERNAL and STATIC bits in flags turns them off is true if both bits are off Spring 201116Chapter 6

17 Bit-fields turns the bits on turns them off Tests the bits Spring 201117Chapter 6


Download ppt "Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped."

Similar presentations


Ads by Google