Docsity

Structures, Unions - Introduction to Programming - Lecture Slides, Slides of Computer Programming

Partial preview of the text

Download Structures, Unions - Introduction to Programming - Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 20

In the Last Two Lectures

 File Handling

  • Sequential Files
  • Random Access Files

Structure

Structure definition struct Student { char name [ 60 ] ; char address [ 100 ] ; Data of the structure double gpa ; } ; Structure name

Keyword

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 ;

Structures

Variables of type structure

Pointers to Structure

Array of Structure

Student s [ 100 ] ; Student *sPtr ; Structures

Student s1 , s2 ; s1 = s2 ; Structures

Example 2 struct Student { char name [ 64 ] ; char course [ 128 ] ; int age ; int 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 ” ) ;

Assignment of structures Student s1 , s2 ; s2 = s1 ;

Passing Structures to Functions