Data structure and
programming I
Topic 12: Structure
1
OBJECTIVE
▪Understand and know how to create new data type that
consist of many other variables
2
Structure
▪ Allow programmers to create a new data type which consists of
other defined data types (int, float, character, string, …)
▪ It could consist of the other structure or itself as well!
3
Create new data type with structure
❑ Structure
▪ Problem: Information of a student (surname, name, age, sex). Suppose that we have 100
students, which data structure can be used to store these data?
▪ Solution: Array for each attribute
const (N : integer) 100
var name[N], surname[N] : sequence of character
Var age[N] : integer
Var sex[N] : char
▪ Inconvenient: The information are scattered from each other in many arrays.
▪ We are difficult to identify all information of a student.
4
Create new data type with structure
❑ Definition
▪ Structure a user-defined data type which allows us to combine data of different types
together. Structure helps to construct a complex data type which is more meaningful
▪ Each element/variable/data in the structure consists in the structure is called
▪ “field” or
▪ “attribute” or
▪ “member”
5
Create new data type with structure
❑ Declaration
▪ Before we can create a variable of a type structure, we need to create the structure
type first.
▪ Syntax to create a structure type:
▪ After we define a structure type, we can
struct identifier declare variables of structure type
attr1: type
Must have the same name
attr2: type
…
Var varName: identifier
attrN: type
end struct 6
Create new data type with structure
❑ Example
▪ Suppose we create a structure for storing a student information
Syntax in C program:
struct student
surname, name: string
age: integer
sex: char
end struct
Var s: student
struct StudentInfo s; 7
Create new data type with structure
❑ How to access to element in the structure
▪ The elements of array are accessed by index while the elements of structure are accessed
by . (period)
▪ Syntax:
structureVariableName.attributeName
8
Create new data type with structure
❑ Example
▪ Suppose we create a structure for storing a student
struct student
surname, name: string
age: integer
sex: char
end struct
Var s: student
s.name “Sok”
read(s.surname)
s.age 20
s.age s.age * 2
9
Create new data type with structure
❑ Example
▪ Suppose we create a structure for storing information of 100 students
struct student
surname, name: sequence of character
age: integer
sex: character
end struct
Var s[100]: student
For(i 0; i<100; i++) do
read(s[i].surname)
read(s[i].name)
read(s[i].sex)
read(s[i].age)
End for 10
Using structure in sub-program
❑ Example
▪ Create a subprogram to find the different age between two students
function diffAge(s1: student, s2: student): integer
begin function
if (s1.age > s2.age) then
return (s1.age – s2.age)
else
return (s2.age – s1.age)
end if
end function
11
Structure in C programming
❑ Examples
12
Example: Using structure
13
Structure in C programming
❑ Using typedef to rename name of data type
▪ Create a structure ▪ Create a structure and give it a short name
▪ Then to create a variable:
▪ Then to create a variable, we don’t need to
struct myStruct ms; use the keyword struct
myStruct ms;
14
Create new data type with structure
❑ Using typedef
S s1;
S s1;
15
Examples
16
Examples
17
Examples
18
Examples
19
An example to create
an Enumeration and
a Structure
in C programming
20
An example to create Structure
(normal and nested structure)
and using it in C programming
HOW TO:
✓ Create a structure, and nested structure
✓ Rename structure using typedef
✓ Create variable of type structure and
Initialize data.
✓ Use and access data in a structure
✓ Access data in a nested structure
21
An example to create Structure
(normal and nested structure)
and using it in C programming
HOW TO:
✓ Create a structure, and nested structure
✓ Initialize data to structure immediately
while creating
✓ Use and access data in a structure
✓ Access data in a nested structure
22
Create new data type with structure
❑ Imbrication of structure / nested structure
▪ Suppose that in the structure type of student, we need to specify the date of birth for each student
▪ So we can declare type structure as the attribute of other structure
▪ Example:
How to use:
struct date
Var s: student
day, month, year: integer
end struct s.dob.year 1997
struct student
surname, name: string
dob: date
age: integer
end struct
23
Q&A
24