130 likes | 141 Views
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.
E N D
Learning Objectives • Structures • Structure types • Pointers and structure types • Structures as function arguments
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
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; };
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"
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
Structure Pitfall • Semicolon after structure definition • ; MUST exist:struct WeatherData{ double temperature; double windVelocity;}; REQUIRED semicolon!
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;
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
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;
Using Pointers with Struct • The -> operator • Shorthand notation • Combines dereference operator, *, anddot operator • Specifies member of class "pointed to"by given pointer
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;
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;
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