structure

Post on 12-Jan-2016

41 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

1

Structure

KnowledgeUnderstand the concept of structure data types

SkillsAble to write application program using structure

2

Introduction Here is the list of students’ details:

– Nama

– nombor kad pengenalan

– nombor matrik

– tahun kemasukan

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 :

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

};

5

Example of Structure Initialization Example:

struct pelajar {char nama[15], noKP[15], matrik[7];int tahunMasuk;

};

Example:struct tarikh {

int hari, bulan, tahun;

};

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;};

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

8

Initialize structure variables

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

31hari

bulan

tahun

tarikhLahir

10

1966

9

Initialize structure variables

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

31hari

bulan

tahun

tarikhLahir

10

1966

struct tarikh { int hari, bulan, tahun;};

10

Initialize structure variables

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

31hari

bulan

tahun

tarikhLahir

10

1966

struct tarikh { int hari, bulan, tahun;};

11

Declaration of structure variables

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

31hari

bulan

tahun

tarikhLahir

10

1966

struct tarikh { int hari, bulan, tahun;};

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;};

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

???

???

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

???

???

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

???

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

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

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

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ?

???

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

? ? ? ? ? ? ?

???

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'

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'

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

???

???

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: _

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_

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_

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

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? _

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'

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'

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'

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'

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

???

???

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 _

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 _

top related