Structure
description
Transcript of Structure
![Page 1: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/1.jpg)
1
Structure
KnowledgeUnderstand the concept of structure data types
SkillsAble to write application program using structure
![Page 2: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/2.jpg)
2
Introduction Here is the list of students’ details:
– Nama
– nombor kad pengenalan
– nombor matrik
– tahun kemasukan
![Page 3: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/3.jpg)
3
C was provided with an alternative
approaches to combined students’ detail.
C used structure
char nama[15], noKP[15], matrik[7];int tahunMasuk;
It could be represented by :
![Page 4: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/4.jpg)
4
Structure Initialization This C’s features specifically used for
combining various types of data
Structure can be initialized by struct. Here is
the general structure of struct:
struct structure_name {takrifan_unsur_1
takrifan_unsur_2
...
takrifan_unsur_n
};
![Page 5: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/5.jpg)
5
Example of Structure Initialization Example:
struct pelajar {char nama[15], noKP[15], matrik[7];int tahunMasuk;
};
Example:struct tarikh {
int hari, bulan, tahun;
};
![Page 6: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/6.jpg)
6
Initialize structure variables Structure variables can be used for
allocating data.
Example:struct pelajar p1;
nama
noKP
tahunMasuk
matrikp1
struct pelajar { char nama[15], noKP[15], matrik[7]; int tahunMasuk;};
![Page 7: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/7.jpg)
7
Initialize structure variables Structure variables can also be declared
at the end of struct declaration.
Example:struct tarikh {
int hari, bulan, tahun;} tarikhLahir, tarikhMasuk;
hari
bulan
tahun
tarikhLahir
hari
bulan
tahun
tarikhMasuk
![Page 8: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/8.jpg)
8
Initialize structure variables
Example:struct tarikh tarikhLahir = {31, 10, 1966};
31hari
bulan
tahun
tarikhLahir
10
1966
![Page 9: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/9.jpg)
9
Initialize structure variables
Example :struct tarikh tarikhLahir = {31, 10, 1966};
31hari
bulan
tahun
tarikhLahir
10
1966
struct tarikh { int hari, bulan, tahun;};
![Page 10: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/10.jpg)
10
Initialize structure variables
Example :struct tarikh tarikhLahir = {31, 10, 1966};
31hari
bulan
tahun
tarikhLahir
10
1966
struct tarikh { int hari, bulan, tahun;};
![Page 11: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/11.jpg)
11
Declaration of structure variables
Example :struct tarikh tarikhLahir = {31, 10, 1966};
31hari
bulan
tahun
tarikhLahir
10
1966
struct tarikh { int hari, bulan, tahun;};
![Page 12: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/12.jpg)
12
Declaration of structure variables Example:
struct pelajar ketua= {"A Bin B",
"651230-01-5298", "A34444", 1990};
'A'' ' 'B''i' 'n' ' ' 'B''\0'
'6''5' '1''2''3''0' '-' '0''1' '-' '5''2''9''8''\0'
'A''3' '4''4''4''4''\0'
1990
nama
noKP
tahunMasuk
matrikketua
struct pelajar { char nama[15], noKP[15], matrik[7]; int tahunMasuk;};
![Page 13: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/13.jpg)
13
Accessing structures Structure item can be accessed by
pointer operator and item name.
Example:struct tarikh hariIni;hariIni.hari = 8;hariIni.bulan = 9;hariIni.tahun = 2003; ???hari
bulan
tahun
hariIni
???
???
![Page 14: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/14.jpg)
14
Accessing structures Structure item can be accessed by
pointer operator and item name.
Example:struct tarikh hariIni;hariIni.hari = 8;hariIni.bulan = 9;hariIni.tahun = 2003; 8hari
bulan
tahun
hariIni
???
???
![Page 15: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/15.jpg)
15
Accessing structures Structure item can be accessed by
pointer operator and item name.
Example :struct tarikh hariIni;hariIni.hari = 8;hariIni.bulan = 9;hariIni.tahun = 2003; 8hari
bulan
tahun
hariIni
9
???
![Page 16: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/16.jpg)
16
Accessing structures Structure item can be accessed by
pointer operator and item name.
Example :struct tarikh hariIni;hariIni.hari = 8;hariIni.bulan = 9;hariIni.tahun = 2003; 8hari
bulan
tahun
hariIni
9
2003
![Page 17: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/17.jpg)
17
Accessing structures Example:
struct pelajar p1;strcpy(p1.nama, "C Bin D");strcpy(p1.noKP, "661122-02-5554");strcpy(p1.matrik, "A11122");p1.tahunMasuk = 1990;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ?
???
nama
noKP
tahunMasuk
matrikp1
![Page 18: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/18.jpg)
18
Accessing structures Example
struct pelajar p1;strcpy(p1.nama, "C Bin D");strcpy(p1.noKP, "661122-02-5554");strcpy(p1.matrik, "A11122");p1.tahunMasuk = 1990;
'C'' ' 'B''i' 'n' ' ' 'D''\0'nama
noKP
tahunMasuk
matrikp1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ?
???
![Page 19: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/19.jpg)
19
Accessing structures Example
struct pelajar p1;strcpy(p1.nama, "C Bin D");strcpy(p1.noKP, "661122-02-5554");strcpy(p1.matrik, "A11122");p1.tahunMasuk = 1990;
'C'' ' 'B''i' 'n' ' ' 'D''\0'
'6''6' '1''1''2''2' '-' '0''2' '-' '5''5''5''4''\0'
nama
noKP
tahunMasuk
matrikp1
? ? ? ? ? ? ?
???
![Page 20: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/20.jpg)
20
Accessing structures Example
struct pelajar p1;strcpy(p1.nama, "C Bin D");strcpy(p1.noKP, "661122-02-5554");strcpy(p1.matrik, "A11122");p1.tahunMasuk = 1990;
'C'' ' 'B''i' 'n' ' ' 'D''\0'
'6''6' '1''1''2''2' '-' '0''2' '-' '5''5''5''4''\0'
nama
noKP
tahunMasuk
matrikp1
???
'A''1' '1''1''2''2''\0'
![Page 21: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/21.jpg)
21
Accessing structures Example
struct pelajar p1;strcpy(p1.nama, "C Bin D");strcpy(p1.noKP, "661122-02-5554");strcpy(p1.matrik, "A11122");p1.tahunMasuk = 1990;
'C'' ' 'B''i' 'n' ' ' 'D''\0'
'6''6' '1''1''2''2' '-' '0''2' '-' '5''5''5''4''\0'
nama
noKP
tahunMasuk
matrikp1
1990
'A''1' '1''1''2''2''\0'
![Page 22: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/22.jpg)
22
Accessing structures Example
struct tarikh tarikhLahir;printf("Masukkan tarikh lahir: ");scanf("%d%d%d", &(tarikhLahir.hari),
&(tarikhLahir.bulan),&(tarikhLahir.tahun));
printf("Tarikh lahir anda: %d/%d/%d\n",tarikhLahir.hari,tarikhLahir.bulan,tarikhLahir.tahun);
???hari
bulan
tahun
tarikhLahir
???
???
![Page 23: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/23.jpg)
23
Accessing structures Example:
struct tarikh tarikhLahir;printf("Masukkan tarikh lahir: ");scanf("%d%d%d", &(tarikhLahir.hari),
&(tarikhLahir.bulan),&(tarikhLahir.tahun));
printf("Tarikh lahir anda: %d/%d/%d\n",tarikhLahir.hari,tarikhLahir.bulan,tarikhLahir.tahun);
???hari
bulan
tahun
tarikhLahir
???
???
Masukkan tarikh lahir: _
![Page 24: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/24.jpg)
24
Accessing structures Example
struct tarikh tarikhLahir;printf("Masukkan tarikh lahir: ");scanf("%d%d%d", &(tarikhLahir.hari),
&(tarikhLahir.bulan),&(tarikhLahir.tahun));
printf("Tarikh lahir anda: %d/%d/%d\n",tarikhLahir.hari,tarikhLahir.bulan,tarikhLahir.tahun);
19hari
bulan
tahun
tarikhLahir
4
2001
Masukkan tarikh lahir: 19 4 2001_
![Page 25: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/25.jpg)
25
Accessing structures Example
struct tarikh tarikhLahir;printf("Masukkan tarikh lahir: ");scanf("%d%d%d", &(tarikhLahir.hari),
&(tarikhLahir.bulan),&(tarikhLahir.tahun));
printf("Tarikh lahir anda: %d/%d/%d\n",tarikhLahir.hari,tarikhLahir.bulan,tarikhLahir.tahun);
19hari
bulan
tahun
tarikhLahir
4
2001
Masukkan tarikh lahir: 19 4 2001Tarikh lahir anda: 19/4/2001_
![Page 26: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/26.jpg)
26
Accessing structures Example
struct pelajar p1;printf("Nama, no KP, matrik, tahun?\n");gets(p1.nama);gets(p1.noKP);gets(p1.matrik);scanf("%d", &(p1.tahun));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ?
???
nama
noKP
tahunMasuk
matrikp1
![Page 27: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/27.jpg)
27
Accessing structures Example
struct pelajar p1;printf("Nama, no KP, matrik, tahun?\n");gets(p1.nama);gets(p1.noKP);gets(p1.matrik);scanf("%d", &(p1.tahun));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ?
???
nama
noKP
tahunMasuk
matrikp1
Nama, no KP, matrik, tahun? _
![Page 28: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/28.jpg)
28
Accessing structures Example
struct pelajar p1;printf("Nama, no KP, matrik, tahun?\n");gets(p1.nama);gets(p1.noKP);gets(p1.matrik);scanf("%d", &(p1.tahun));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ?
???
nama
noKP
tahunMasuk
matrikp1
Nama, no KP, matrik, tahun? E Bin F_
'E'' ' 'B''i' 'n' ' ' 'F''\0'
![Page 29: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/29.jpg)
29
Accessing structures Example
struct pelajar p1;printf("Nama, no KP, matrik, tahun?\n");gets(p1.nama);gets(p1.noKP);gets(p1.matrik);scanf("%d", &(p1.tahun));
? ? ? ? ? ? ?
???
nama
noKP
tahunMasuk
matrikp1
Nama, no KP, matrik, tahun? E Bin F711010-03-5100_
'E'' ' 'B''i' 'n' ' ' 'F''\0'
'7''1' '1''0''1''0' '-' '0''3' '-' '5''1''0''0''\0'
![Page 30: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/30.jpg)
30
Accessing structures Example
struct pelajar p1;printf("Nama, no KP, matrik, tahun?\n");gets(p1.nama);gets(p1.noKP);gets(p1.matrik);scanf("%d", &(p1.tahun));
???
nama
noKP
tahunMasuk
matrikp1
Nama, no KP, matrik, tahun? E Bin F711010-03-5100A23324_
'E'' ' 'B''i' 'n' ' ' 'F''\0'
'7''1' '1''0''1''0' '-' '0''3' '-' '5''1''0''0''\0'
'A''2' '3''3''2''4''\0'
![Page 31: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/31.jpg)
31
Accessing structures Example
struct pelajar p1;printf("Nama, no KP, matrik, tahun?\n");gets(p1.nama);gets(p1.noKP);gets(p1.matrik);scanf("%d", &(p1.tahun));
1997
nama
noKP
tahunMasuk
matrikp1
E Bin F711010-03-5100A233241997_
'E'' ' 'B''i' 'n' ' ' 'F''\0'
'7''1' '1''0''1''0' '-' '0''3' '-' '5''1''0''0''\0'
'A''2' '3''3''2''4''\0'
![Page 32: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/32.jpg)
32
Accessing structures Structure can be assigned to another
structure
Example:struct tarikh hariIni, salinan;scanf("%d%d%d", &(hariIni.hari),
&(hariIni.bulan),&(hariIni.tahun));
salinan = hariIni;
???hari
bulan
tahun
salinan
???
???
???hari
bulan
tahun
hariIni
???
???
![Page 33: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/33.jpg)
33
Accessing structures Structure can be assigned to another
structure
Example:struct tarikh hariIni, salinan;scanf("%d%d%d", &(hariIni.hari),
&(hariIni.bulan),&(hariIni.tahun));
salinan = hariIni;
???hari
bulan
tahun
salinan
???
???
8hari
bulan
tahun
hariIni
9
2003
8 9 2003 _
![Page 34: Structure](https://reader036.fdocuments.us/reader036/viewer/2022070410/56814521550346895db1e3a5/html5/thumbnails/34.jpg)
34
Accessing structures Structure can be assigned to another
structure
Example:struct tarikh hariIni, salinan;scanf("%d%d%d", &(hariIni.hari),
&(hariIni.bulan),&(hariIni.tahun));
salinan = hariIni;
8hari
bulan
tahun
salinan
9
2003
8hari
bulan
tahun
hariIni
9
2003
8 9 2003 _