90 likes | 204 Vues
This guide provides an overview of structures (structs) in C programming, focusing on their definition, initialization, and common usage patterns, including combining multiple structs. Understand how to correctly define a struct with member variables, initialize them appropriately, and use the dot operator to access these members. Learn about logical errors that can occur during initialization and see examples demonstrating how to include structs within other structs. Perfect for beginners looking to enhance their programming skills.
E N D
HKUST SummerProgramming Course 2008 Structure
Overview • Structure • Initialize a struct • Quiz • Semicolon in a struct definition • Combining structs
Struct • A collection of values of different types (a structure) struct FruitCollection { int quantity; char size; // (L)arege, (M)iddle and (S)mall }; • Usually be placed under global scope, it can also be put in another scopes. • Use dot operator (.) to access member variables. FruitCollection pineapple; pineapple.quantity = 6; pineapple.size = ‘L’; • Can have more than one struct in a single program
Initialize a struct • Initialize by a list of values according to the sequence of variables in the struct definition. FruitCollection orange = { 100, ‘M’ }; • If number of values is MORE than the struct requires, error will occur. • If number of values is LESS than the struct requires, zero value will be given to the rest of variables. • In contrast, C++ won’t initialize local variables • Copy from other objects of the same type. FruitCollection apple; apple = orange; //apple is now {100, ‘M’}
Quiz int main() { struct Date{ int month, day, year; } Date dueDate = { 31, 12, 2006 }; return 0; } • What’s wrong with the program?
Semicolon in a struct definition • A semicolon must be put at the end of a struct definition, otherwise the program can’t compile. struct Date { int month, day, year; }; //<- remember to add this semicolon • A useful feature related to this semicolon. struct fruit { int quantity; char size; } apple, orange; // variables of fruit type • Variables of this struct can be declared immediately after the definition.
Logical error • The order of initialization of data member is the same as the order of declaration of data member • The previous example will initialize the date as Year = 2006, Month = 31, Day = 12
Combining structs • A struct can make use of another struct to be the type of its member variables. // fruitCollection was defined in previous slides struct fruitBasket { fruitCollection apple; fruitCollection orange; }; int main() { fruitBasket A; A.apple.quantity = 3; A.apple.size = ‘L’; A.orange.quantity = 6; … } • To access the member variable of a struct inside another struct, we can use the dot operator twice. (Dot operator also called member access operator)
Combining structs • A struct can also be defined inside another struct. struct bookBorrow { string borrower; string bookTitle; // struct inside another struct struct Date { int day, month, year; } dueDate; }; • Be careful when you try to initialize it, bookBorrow bk001={"Adam", "The Wizard Book", {31,12,2006}}; cout << bk001.dueDate.day << endl; // output is 31