structure

34
1 Structure Knowledge Understand the concept of structure data types Skills Able to write application program using structure

Upload: dinh

Post on 12-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

Knowledge Understand the concept of structure data types Skills Able to write application program using structure. Structure. Introduction. Here is the list of students’ details: Nama nombor kad pengenalan nombor matrik tahun kemasukan. It could be represented by :. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Structure

1

Structure

KnowledgeUnderstand the concept of structure data types

SkillsAble to write application program using structure

Page 2: Structure

2

Introduction Here is the list of students’ details:

– Nama

– nombor kad pengenalan

– nombor matrik

– tahun kemasukan

Page 3: Structure

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

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

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

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

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

8

Initialize structure variables

Example:struct tarikh tarikhLahir = {31, 10, 1966};

31hari

bulan

tahun

tarikhLahir

10

1966

Page 9: Structure

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 _