310 likes | 324 Views
Learn how to implement structured structures in C language with practical examples and detailed lecture summaries. Explore typedef, syntax initialization, function returns, and differences between structures and arrays. Enhance your understanding of struct manipulation and deep versus shallow copy. Access lecture slides at the provided link for additional study materials.
E N D
CS1010 Discussion Group 11 Week 12 – Structured Structures
HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/
Lab 6 Last lab!
Ex 1 Practical Exam 2 • See the example clearly. SDU388 SDU0388 • Leading zeros are not printed out in the actual car plate! • A few ways: • Hard code and check the length and change the formula accordingly • 9 4 5 4 3 2 9 4 4 3 2 9 4 3 2 9 4 2 • Zero padding of 388 to 0388 (a bit complicated in C) • Use a weights array { 9 4 5 4 3 2 } then add a positive offset based on the difference in length (MAX_LEN – len)
Ex 1 Practical Exam 2 • Check sample runs! When printing Invalid Car Type, don’t print about registration number anymore. • Don’t think too much. TT
Ex 2 Practical Exam 2
Ex 2 Practical Exam 2
Structures Lecture Summary • typedefis a keyword used in C language to assign alternative names to existing types. • You are assigning “result_t” name to this user-defined struct type and you end with a semi-colon • Primitive types are int, char, long, float, double etc • You are defining your own “type”! Type is not a variable typedefstruct{ intstuNum; float score; char grade; } result_t;
Structures Lecture Summary • Define your own types before function prototypes but after pre-processor directives • Then you can use it like as if it is yet another type (same like int, float, double etc) result1 and result2 are variables of type “result_t” typedefstruct{ intstuNum; float score; char grade; } result_t; result_t result1, result2;
Structures Lecture Summary • Syntax to initialise is like array initialisation even for a “structure inside a structure” • Then access members of a structure using the dot (.) operator typedefstruct { int day, month, year; } date_t; typedefstruct { intcardNum; date_t birthday; } card_t; card_tcard1= {888888, {31, 12, 2020}}; card_t card2 = { 666666, {30, 6} }; card2.birthday.year = 2021;
Structures Lecture Summary • Since a function can return a type, it can return a struct! • returned struct of type “result_t” from func is copied over to result variable which is also of type “result_t” • This way, you can return 2 or more values from a function using structs and don’t need to use pointers result_tfunc( ... ) { ... } result_t result; result = func( ... );
Structures vs Arrays Lecture Summary • Structure name refers to the entire structure. Array name “decays” to a constant pointer to the address of the first element of the array • Unlike arrays we can do assignment with structures • result1 = result2 will really copy the data over by value • Now if you change result1.stuNum, It will not affect result2. result2.stuNum = result1.stuNum; result2.score = result1.score; result2.grade = result1.grade;
Structures vs Arrays Lecture Summary • Problem occurs when struct contain pointers. This is a “shallow copy” or “member-wise copy” • The pointers are copied over but not the contents of the pointers. Both structs will now contain pointers to the same location. Any changes made via one pointer will be seen by the other pointer. (To copy over content of pointers will be called a “deep copy”) • For structs that contain arrays, the array contents are copied over to the other array in the other struct, element by element • Arrays cannot be “assigned” and the complier can only copy over the elements
Structures - More Lecture Summary typedefstruct{ char name[12]; int age; char gender; } player_t; • Structures with strings (char arrays) • Same as before! It is a normal char array player_t player3; printf("Enter name, age and gender: "); scanf("%s %d %c", player3.name, &player3.age, &player3.gender);
Structures - More Lecture Summary • When structures are passed into a function, • It is copied over just like an assignment result1 = result2 • Original struct variable will not be modified. You need to return the struct • Different from passing array into a function. • Array name is like a constant pointer to the first element while the structure name is the entire structure itself! • To modify the original struct, pass in the address of the struct to the function
-> Structures -> Lecture Summary • Assume sptr is a pointer to some structure • The dot operator has higher precedence than the pointer deference operator • The following will try to access the thing pointed to by sptr.gpa, which does not work, because sptr is not a struct: • *sptr.gpa = 4.0; // Same as *(sptr.gpa) = 4.0; • The parenthesis operator can be used to override the precedence, but that is awkward: (*sptr).gpa = 4.0; • Instead a new operator is introduced, the "pointer to structure member reference operator", Arrow Operator • sptr->gpa = 4.0; // Same as (*sptr).gpa = 4.0;
Tutorial 1 What to expect? Compilation error. Variable one cannot be assigned to two due to incompatible types, even though both one_t and two_t are defined with the same member types and names. But type “one_t” is just not type “two_t” no matter how it tries!!!
Tutorial 2 More than one answer! The target is s.a. Since scanf() requires the address of the target, the argument for the scanf() function is hence &(s.a) Since dot operator has a higher precedence than the address operator, the parentheses can be omitted, to become &s.a
Tutorial 3 Use structures to return 2 or more outputs from a function instead of pointers.
Tutorial 3 This result variable is a temp variable!
Tutorial 4 More than one answer! the target is (*p).a The alternative way of writing is p->a The target is NOT *p.a because dot has a higher precedence than *, hence *p.a is equivalent to *(p.a). Since scanf() requires the address of the target, the argument for the scanf() function is hence &((*p).a) or &(p->a)
Tutorial 5 How to read in data into the array of tile_t?
Tutorial 5 scan_tiles = What is inside the for loop?
Tutorial 5 Process the struct of type tile_t
Tutorial 5 Find max and min cost at the same time
Tutorial 6 Array of another struct in struct
Tutorial 6 Array of another struct in struct
Tutorial 6 Iterating through the struct array in the struct
Tutorial 7 How to create the timeline? How to calculate the longest lesson? How to detect a free period? How to find a period with the most concurrent lessons?
Tutorial 7 How to create the endpoints array? How to find the free periods? How to not include the gap in between zero and the first lesson, the gap between last lesson and the end of the array