CS201-20 - Introduction to Programming Lecture 20 In the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 20 In the Last Two Lectures File Handling – Sequential Files – Random Access Files Today’s Lecture Structures Unions Structure ? Keyword Structure definition Structure name struct Student { char name [ 60 ] ; char address [ 100 ] ; Data of the structure double gpa ; } ; } Student s1 , s2 , s3 ; } Variable Names Structure Name Structure struct address { char streetAddress [ 100 ] ; char city [ 30 ] ; char country [ 30 ] ; } Structure struct Student { char name [ 60 ] ; address add ; double gpa ; } s1 , s2 , s3 ; Example 1 struct Card { char *suit ; char *value ; } ; Structures Variables of type structure Pointers to Structure Array of Structure Structures Student s [ 100 ] ; Student *sPtr ; Structures Student stdnt1 , stdnt2 ; stdnt1 + stdnt2 ; //Wrong Structures Student s1 , s2 ; s1 = s2 ; Example 2 struct Student { char name [ 64 ] ; char course [ 128 ] ; int age ; int year ; } ; Initializing Structures Student s1 = { “Amara”,“cs201”,“19”, “2002” } ; Name Course name age year Initializing Structures s1.name ; s1.course ; s1.age ; s1.year ; Initializing Structures s1.age = 20 ; s1.name = “Abbas Ali ” ; //Wrong strcpy ( s1.name , “Abbas Ali ” ) ; Accessing structure members cout << s1.name ; cout << s1.course ; cout << s1.age ; cout << s1.year ; Assignment of structures Student s1 , s2 ; s2 = s1 ; Passing Structures to Functions Passing Structures to Functions Call by Reference X Call by value int Square ( int x ) ; x = Square ( 10 ) ; Simple Variable of type Structure Pointer to Structure Arrays of Structures Function that returns a Structure Pass the Structure to functions Structures Pointers to Structure Pointers to Structure Student *sPtr , s1 ; sPtr = &s1 ; Pointers to Structure sPtr.name ; Wrong Pointers to Structure *sPtr.name ; Pointers to Structure *( sPtr ).name ; Pointers to Structure *sPtr ­>name ; Same as s1.name Arrays of Structures Arrays of Structure Student s [ 100 ] ; s [ 0 ].name ; s [ 1 ].name ; s [ 2 ].name ; . . . s [ 99 ].name ; sizeof ( s1 ) ; Example 3 struct Student { char firstName [ 30 ] ; char lastName [ 30 ] ; char course [ 15 ] ; char rollNo [ 10 ] ; int age ; float gpa ; } ; Student s [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { cout << “Please enter the student's last name : " ; cin >> s [ i ].lastName ; cout << “Please enter the student's first name : " ; cin >> s [ i ].firstName ; cout << " Please enter the student's course : " ; cin >> s [ i ].course ; cout << " Please enter the student's Roll No. : " ; cin >> s [ i ].rollNo ; cout << " Please enter the student's grade : " ; cin >> s [ i ].grade ; cout << " Please enter the student's age : " ; cin >> s [ i ].age ; cout << " Please enter the student's GPA : " ; cin >> s [ i ].gpa ; } Example 3 Example 4 Student getData ( ) ; Return Type Function Name Example 4 ifstream inFile ( “myText.txt” ) ; void main ( ) { Student s1 ; s1 = getData ( ) ; inFile.close ( ) ; } Student getData ( ) { Student tempStudent ; inFile >> tempStudent.firstName ; inFile >> tempStudent.lastName ; inFile >> tempStudent.course ; inFile >> tempStudent.rollNo ; inFile >> tempStudent.age ; inFile >> tempStudent.gpa ; return tempStudent ; } Example 4 Union Union union intOrChar { int i ; char c ; } ; Union Memory C i Both Variable occupy the same space in memory Union union intOrDouble { int ival ; double dval ; } ; Union Memory iVal dVal Both Variable occupy the same space in memory Union u1.ival = 10 ; cout << u1.ival ; cout << u1.dval ; incorrect value intOrDouble u1 ; Union u1.dval = 100.0 ; cout << u1.ival ; incorrect value Example 5 char c ; int x ; x = ‘a’ ; cout << x ; x = x * 256 cout << x ; : x = x +’b’ ; Example 5 cout << x ; Example 5 Union iandc { char c [ 4 ] ; int x ; } ; Today we studied Structures Unions ...
View Full Document

This note was uploaded on 01/12/2010 for the course CS CS 201 taught by Professor Dr.naveedmalik during the Spring '09 term at Virtual University of Pakistan.

Ask a homework question - tutors are online