Chapter 08 - Structure
Chapter 08 - Structure
STRUCTURE
1
OBJECTIVES:
2
Structure
5 7
1234 F A T I M A H /0 3.7
4
Structure Declaration and Definition
General Format:
struct <structName>
{
type1 field1;
type2 field2;
. . .
};
• To define its variables, use the structure tag (name) as the
variable’s type.
5
Structure Declaration and Definition
Example :
struct
structure tag
{
int ; structure members
string ;
short ;
double ;
bill
};
studentID
STUDENT bill;
name
yearInSchool
gpa
6
Structure Initialization
• The rules for structure initialization are similar to the rules for array
initialization:
(1) the initializers are enclosed in braces and separated by commas;
(2) the initializers must match their corresponding types in the structure
declaration;
SAMPLE sam2 = { 7, 3 };
sam2
8
Referencing Individual Field
• We can read data into and write data from structure members just
as we can from individual variables.
• For example the value for the field of the sample structure can be
read from the keyboard and placed in sam2 using the input statement
below.
9
Structure Operation
• The structure is an entity that can be treated as a whole.
• However, only one operation, assignment is allowed on the structure
itself. In other words, a structure can only be copied to another structure of
the same type using the assignment operator.
• Example :
10
struct STUDDATA
{
int id;
char name[20];
float gradePoint;
};
int main( )
{ Exercise 1
STUDDATA studBITG1113;
struct DATE
{
int month;
int day;
int year;
};
struct TIME
{
int hour;
int min;
int sec;
};
struct STAMP
{
DATE date;
TIME time;
};
13
STAMP stamp;
• It is possible to nest the same structure type more than once
in a declaration.Example :
struct JOB
{
STAMP startTime;
STAMP endTime;
};
JOB job;
14
Referencing Nested Structure
• When access a nested structure, we must include each level
from the highest (stamp) to the component being referenced.
15
Structure Containing Array
• Structures can have one or more arrays as members.
/*Global declarations */
struct PUPIL
{
char name[26];
int midterm[3];
int final;
};
16
PUPIL student;
struct STUDDATA
{ A struct with array
char name[20];
float test[3];
members
float ass[5];
float quiz[2];
float final;
float total;
float project;
Exercise 2
};
int main( )
{
STUDDATA studBITG1113;
float totTest =0, totAss = 0, totQuiz=0;
19
Array of Structure
• As a programmer, you will encounter many situations that
require you to create an array of structures.
20
Example : Array of Structure
struct PELAJAR
{
int id;
char name[31];
float project_mark;
int test_mark;
int final_mark;
char gred;
};
PELAJAR rekod_pelajar[3];
OR with initialization :
struct pelajar rekod_pelajar[] = {
{1342, "Zulaiha Ismail", 10.2, 10, 20, ‘F’},
{1343, "Aina Ahmad", 51.4, 60, 60, ‘C’},
21
{1344, "Maria Musa", 90.0, 99, 99, ‘A’}
};
Example : Array of Structure
22
struct STUDDATA
{ Array with struct as
char name[20];
float test[3]; its elements
float ass[5];
float quiz[2];
float final;
float total;
float project; Exercise 3
};
int main( )
{
STUDDATA studBITG1113[3];
float totTest = 0, totAss = 0, totQuiz = 0;