week8 - Unix C + GNU HTML Perl Python Structures and...

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

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 1 Structures and Pointers Comp-206 : Introduction to Software Systems Week 8 Joseph Vybihal Computer Science McGill University
Background image of page 1

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

View Full DocumentRight Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 2 Announcements
Background image of page 2
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 3 Part 1 C Structures
Background image of page 3

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

View Full DocumentRight Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 4 Structures and Unions Structures are a data type composed of several other data types. Think of it as a container, a variable that has variables inside it. You can define new structures using the struct keyword. struct course_record { struct TYPENAME { int number_of_students; FIELDS; char name_professor[100]; } VARNAME; char location_building[100]; int location_room; } cs206;
Background image of page 4
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 5 Using a structure To use a structure, you need to instantiate a copy of it. All you need to do is to declare the variable for the instance. struct course cs206; You can then fill it with data. cs206.number_of_student = 60; cs206.name_professor = “Alex”; cs206.location_building = “MacDonald”; cs206.location_room = 328; With structures, you can declare the variable and initialize it with data in one command. struct course cs206 = {60, “Alex”, “MacDonald”, 328};
Background image of page 5

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

View Full DocumentRight Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 6 typedef and struct You can use typedef to define the structure as a new type. typedef struct course { int number_of_students; char name_professor[100]; char location_building[100]; int location_room; } TYPENAME; When creating a variable of this type, you no longer need to specify the struct keyword. TYPENAME cs206;
Background image of page 6
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 7 Question How to build an array of student structures? And use it?
Background image of page 7

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

View Full DocumentRight Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 8 Answer struct student_rec { char name[100]; int age; float gpa; } students[50]; students[2].age = 20; gets(student[2].name); for(i=0;i<50;i++) printf(“%s”,students[i].name);
Background image of page 8
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 9 Polymorphism in C union TYPENAME { FIELDS; } VAR; union multiple { int x; char y; float d; } z; How does this look in RAM?
Background image of page 9

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

View Full DocumentRight Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 Software Systems 10 Question How can we build a polymorphic data structure with: Students Teachers Staffs
Background image of page 10
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 39

week8 - Unix C + GNU HTML Perl Python Structures and...

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

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