init_struct

init_struct - Programming Initializing Data of Struct Type...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Initializing Data of Struct Type
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type By assignment during declaration; example : struct StudentRecord { // student record double totalgrade; char name[name_size], id[id_size]; int grade[no_grades]; }; StudentRecord student1 = { 0.0, "CHAN Tai Man", "12345678", {80, 67, 34, 67} };
Background image of page 2
2 Prog. Fundamentals: initialize struct type/ Slide 3 Initialization of a “nested” structure: struct Point { int x, y; }; //defines the coordinates of a point struct Triangle { Point vertex[3]; double area; }; //defines a triangle Triangle tlist[2] = { {{{0, 0}, {1, 0}, {0, 1}}, 0.5}, // 1st Triangle {{{2, 0}, {3, 0}, {3, 1}}, 0.5} // 2nd Triangle }; Ex. 11: Initialize Data of struct Type
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prog. Fundamentals: initialize struct type/ Slide 4 Ex. 12: Using struct Type in Functions 1. As parameters: both pass-by-value and pass-by-reference are supported. 2. As type of function: assembly of return value needed. Example of case 2: StudentRecord shrink_wrap( // a function with const char sname[], // name shrink_wrap of const char sid[], // type StudentRecord const int sgrade[]) { StudentRecord temp; temp.totalgrade = 0; strcpy (temp.name, sname); strcpy (temp.id, sid); for ( int index=0; index<no_grades; index++) temp.grade[index] = sgrade[index]; return temp; } // Defines an initialization function
Background image of page 4
2 Prog. Fundamentals: initialize struct type/ Slide 5 Ex. 12: Using struct Type in Functions StudentRecord student1; int scores[4] = {80, 67, 34, 67}; student1 = shrink_wrap ("CHAN Tai Man", "12345678", scores}; has the same effect of: StudentRecord student1 = { 0.0, "CHAN Tai Man", "12345678", {80, 67, 34, 67} };
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prog. Fundamentals: initialize struct type/ Slide 6
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/07/2011 for the course COMP 102 taught by Professor Daniel during the Spring '08 term at HKUST.

Page1 / 19

init_struct - Programming Initializing Data of Struct Type...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online