0% found this document useful (0 votes)
42 views49 pages

CS201 20

The document discusses structures, unions, and pointers in C programming. It provides examples of defining and initializing structures, accessing structure members, passing structures to functions, arrays of structures, and pointers to structures. It also discusses unions and how variables in a union share the same memory location.

Uploaded by

bc200403674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views49 pages

CS201 20

The document discusses structures, unions, and pointers in C programming. It provides examples of defining and initializing structures, accessing structure members, passing structures to functions, arrays of structures, and pointers to structures. It also discusses unions and how variables in a union share the same memory location.

Uploaded by

bc200403674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49

Introduction to Programming

Lecture 20
In the Last Two Lectures

 File Handling
– Sequential Files
– Random Access Files
Today’s Lecture

 Structures
 Unions
Structure

?
Structure definition
Keyword

struct Student
Structure
name

}
char name [ 60 ] ;
char address [ 100 ] ; Data of the structure

double gpa ;
};
Student s1 , s2 , s3 ;
Structure Name

}Variable Names
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
year
Course age
name
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 value
Call by Reference X
int Square ( int x ) ;
x = Square ( 10 ) ;
Structures
 Simple Variable of type
Structure
 Pointer to Structure
 Arrays of Structures
 Function that returns a
Structure
 Pass the Structure to functions
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 ;
};
Example 3
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 4

Student getData ( ) ;
Return
Type
Function
Name
Example 4
ifstream inFile ( “myText.txt” ) ;
void main ( )
{
Student s1 ;
s1 = getData ( ) ;
inFile.close ( ) ;
}
Example 4
Student getData ( )
{
Student tempStudent ;
inFile >> tempStudent.firstName ;
inFile >> tempStudent.lastName ;
inFile >> tempStudent.course ;
inFile >> tempStudent.rollNo ;
inFile >> tempStudent.age ;
inFile >> tempStudent.gpa ;
return tempStudent ;
}
Union
Union
union intOrChar
{
int i ;
char c ;
};
Union
Memory

Both Variable
i occupy the
same space in
memory
Union
union intOrDouble
{
int ival ;
double dval ;
};
Union
Memory

iVal

Both Variable
dVal
occupy the
same space in
memory
Union
intOrDouble u1 ;
u1.ival = 10 ;
cout << u1.ival ;
cout << u1.dval ; incorrect value
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 ;
:
Example 5
 x = x +’b’ ;
 cout << x ;
Example 5
Union iandc
{
char c [ 4 ] ;
int x ;
};
Today we studied

 Structures
 Unions

You might also like