{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

init_struct

# init_struct - Programming Initializing Data of Struct Type...

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

Programming Initializing Data of Struct Type

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

View Full Document
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} };
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

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

View Full Document
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
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} };

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

View Full Document
2 Prog. Fundamentals: initialize struct type/ Slide 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern