1 / 13

Learning Objectives

Learn about structures in C programming, including the different types, accessing structure members, assigning and initializing structures, using pointers with structures, and passing structures as function arguments.

greerj
Download Presentation

Learning Objectives

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. Learning Objectives • Structures • Structure types • Pointers and structure types • Structures as function arguments

  2. Structures • Aggregate data type: struct • Aggregate meaning "grouping" • Recall array: collection of values of same type • Structure: collection of values of different types • Treated as a single item, like arrays • Major difference: Must first "define" struct • Prior to declaring any variables

  3. Structure Types • Define struct globally (typically) • No memory is allocated • Just a "placeholder" for what our struct will "look like" • Definition: struct CDAccount Name of new struct "type" { double balance;  member names double interestRate; int term; };

  4. Declare Structure Variable • With structure type defined, now declarevariables of this new type:CDAccountaccount_1; • Just like declaring simple types • Variable account now of type CDAccount • It contains "member values" • Each of the struct "parts"

  5. Accessing Structure Members • Dot Operator to access members • account_1.balance • account_1.interestRate • account_1.term • Called "member variables" • The "parts" of the structure variable • Different structs can have same name member variables • No conflicts

  6. Structure Pitfall • Semicolon after structure definition • ; MUST exist:struct WeatherData{ double temperature; double windVelocity;};  REQUIRED semicolon!

  7. Structure Assignments • Simple assignments are legal between instances of same structure: apples = oranges; • Simply copies each member variable from applesinto member variables from oranges • Sometime confusing, not recommend to use • Assign field by field, more accurate and clear • apple.price = oragnge.price; • apple.producer = orange.produce;

  8. Initializing Structures • Can initialize at declaration • Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003}; • Declaration provides initial data to all three member variables • Not commonly used

  9. Initializing Structures (continued) • Initialize after declaration (more commenly used)struct Date { int month; int day; int year; }; Date dueDate; dueDate.month =12; dueDate.day = 31; Duedate.year = 2003;

  10. Using Pointers with Struct • The -> operator • Shorthand notation • Combines dereference operator, *, anddot operator • Specifies member of class "pointed to"by given pointer

  11. Using Pointers with Struct (continued) struct Date { int month; int day; int year; }; Date dueDate; Date * P = &dueDate P->month =12; P->day = 31; P->year = 2003;

  12. Using Pointers with Struct (continued) struct Date { int month; int day; int year; }; Date * P = new Date; P->month =12; P->day = 31; P->year = 2003;

  13. Structures as Function Arguments • Passed like any simple data type • Pass-by-value • Pass-by-reference • Pass-by-pointer • Can also be returned by function • Return-type is structure type • Return statement in function definitionsends structure variable back to caller

More Related