Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1010 Discussion Group 11 Week 12 – Structured Structures.

Similar presentations


Presentation on theme: "CS1010 Discussion Group 11 Week 12 – Structured Structures."— Presentation transcript:

1 CS1010 Discussion Group 11 Week 12 – Structured Structures

2 Slides are at http://www.comp.nus.edu.sg/~yanhwa/
HELLO! Slides are at

3 Lab 6 Last lab!

4 Ex 1 See the example clearly. SDU388  SDU0388
Practical Exam 2 Ex 1 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 2 Zero padding of 388 to 0388 (a bit complicated in C) Use a weights array { } then add a positive offset based on the difference in length (MAX_LEN – len)

5 Practical Exam 2 Ex 1 Check sample runs! When printing Invalid Car Type, don’t print about registration number anymore. Don’t think too much. TT

6 Practical Exam 2 Ex 2

7 Practical Exam 2 Ex 2

8 Lecture Summary Structures typedef is 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 typedef struct { int stuNum; float score; char grade; } result_t;

9 result1 and result2 are variables of type “result_t”
Lecture Summary Structures 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” typedef struct { int stuNum; float score; char grade; } result_t; result_t result1, result2;

10 Lecture Summary Structures Syntax to initialise is like array initialisation even for a “structure inside a structure” Then access members of a structure using the dot (.) operator typedef struct { int day, month, year; } date_t; int cardNum; date_t birthday; } card_t; card_t card1 = {888888, {31, 12, 2020}}; card_t card2 = { , {30, 6} }; card2.birthday.year = 2021;

11 Structures Since a function can return a type, it can return a struct!
Lecture Summary Structures 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_t func( ... ) { ... } result_t result; result = func( ... );

12 Lecture Summary Structures vs Arrays 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;

13 Lecture Summary Structures vs Arrays 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

14 Structures - More Structures with strings (char arrays)
Lecture Summary Structures - More typedef struct { 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);

15 Structures - More When structures are passed into a function,
Lecture Summary Structures - More 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

16 -> Structures ->
Lecture Summary -> Structures -> 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;

17 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!!!

18 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

19 Tutorial 3 Use structures to return 2 or more outputs from a function instead of pointers.

20 Tutorial 3

21 Tutorial 3 This result variable is a temp variable!

22 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)

23 How to read in data into the array of tile_t?
Tutorial 5 How to read in data into the array of tile_t?

24 scan_tiles = What is inside the for loop?
Tutorial 5 scan_tiles = What is inside the for loop?

25 Process the struct of type tile_t
Tutorial 5 Process the struct of type tile_t

26 Find max and min cost at the same time
Tutorial 5 Find max and min cost at the same time

27 Array of another struct in struct
Tutorial 6 Array of another struct in struct

28 Array of another struct in struct
Tutorial 6 Array of another struct in struct

29 Iterating through the struct array in the struct
Tutorial 6 Iterating through the struct array in the struct

30 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?

31 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


Download ppt "CS1010 Discussion Group 11 Week 12 – Structured Structures."

Similar presentations


Ads by Google