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

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
For(int i = 1; i
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Introduction to Programming Lecture 39. Copy Constructor.
Pointer to Structure. Structure variable can be access using pointers int a=10,*p; Here p  is an integer type pointer variable, p can hold the address.
Structures Spring 2013Programming and Data Structure1.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Win32 Programming Lesson 4: Classes and Structures.
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
1 Review of Chapter 10: String and Pointers. 2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
1 Dimension of String Array An array of string is two dimension array.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
1 Structures Containing Arrays This type of structure can be declared in C. In other word, each item in array is a structure itself. Example: struct tarikh.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Array, Structure and Union
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 CS161 Introduction to Computer Science Topic #15.
STRUCTURES. C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
POINTER I POINTER DECLARATION DECLARATION BY DYNAMIC POINTER.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
Chapter 10-1: Structure.
Visit for more Learning Resources
Pointer Data Type and Pointer Variables
Buy book Online -
CSC 253 Lecture 8.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
S. Kiran, PGT (CS) KV, Malleswaram
CSC 253 Lecture 8.
Universiti Teknologi MARA
Structure ការណែនាំអំពី Structure
Universiti Teknologi MARA
Character & String Knowledge
CS148 Introduction to Programming II
Local Variables, Global Variables and Variable Scope
FYP I TAJUK PROJEK Rujukan Abstrak Pengenalan Perbandingan Projek
CS111 Computer Programming
Dynamic Memory A whole heap of fun….
C Programming Lecture-13 Structures
Structures In C Programming By Rajanikanth B.
C Programming Pointers
A simple function.
Creating and Using Pointer Variables in C++ By: Ed Brunjes
The Stack.
Pointer Data Type and Pointer Variables
C Programming Lecture-14 Unions
Pointer Data Type and Pointer Variables
CSCE 206 Lab Structured Programming in C
CS148 Introduction to Programming II
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

1 Structure Knowledge Understand the concept of structure data types Skills Able 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 matrik p1 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}; 31 hari bulan tahun tarikhLahir

9 Initialize structure variables Example : struct tarikh tarikhLahir = {31, 10, 1966}; 31 hari bulan tahun tarikhLahir struct tarikh { int hari, bulan, tahun; };

10 Initialize structure variables Example : struct tarikh tarikhLahir = {31, 10, 1966}; 31 hari bulan tahun tarikhLahir struct tarikh { int hari, bulan, tahun; };

11 Declaration of structure variables Example : struct tarikh tarikhLahir = {31, 10, 1966}; 31 hari bulan tahun tarikhLahir struct tarikh { int hari, bulan, tahun; };

12 Declaration of structure variables Example: struct pelajar ketua = {"A Bin B", " ", "A34444", 1990}; 'A'' 'B''i''n'' 'B''\0' '6''5''1''2''3''0''-''0''1''-''5''2''9''8''\0' 'A''3''4' '\0' 1990 nama noKP tahunMasuk matrik ketua 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; 8 hari 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; 8 hari 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; 8 hari bulan tahun hariIni

17 Accessing structures Example: struct pelajar p1; strcpy(p1.nama, "C Bin D"); strcpy(p1.noKP, " "); strcpy(p1.matrik, "A11122"); p1.tahunMasuk = 1990; ??????????????? ??????????????? ??????? ??? nama noKP tahunMasuk matrik p1

18 Accessing structures Example struct pelajar p1; strcpy(p1.nama, "C Bin D"); strcpy(p1.noKP, " "); strcpy(p1.matrik, "A11122"); p1.tahunMasuk = 1990; 'C'' 'B''i''n'' 'D''\0' nama noKP tahunMasuk matrik p1 ??????????????? ??????? ???

19 Accessing structures Example struct pelajar p1; strcpy(p1.nama, "C Bin D"); strcpy(p1.noKP, " "); strcpy(p1.matrik, "A11122"); p1.tahunMasuk = 1990; 'C'' 'B''i''n'' 'D''\0' '6' '1' '2' '-''0''2''-''5' '4''\0' nama noKP tahunMasuk matrik p1 ??????? ???

20 Accessing structures Example struct pelajar p1; strcpy(p1.nama, "C Bin D"); strcpy(p1.noKP, " "); strcpy(p1.matrik, "A11122"); p1.tahunMasuk = 1990; 'C'' 'B''i''n'' 'D''\0' '6' '1' '2' '-''0''2''-''5' '4''\0' nama noKP tahunMasuk matrik p1 ??? 'A''1' '2' '\0'

21 Accessing structures Example struct pelajar p1; strcpy(p1.nama, "C Bin D"); strcpy(p1.noKP, " "); strcpy(p1.matrik, "A11122"); p1.tahunMasuk = 1990; 'C'' 'B''i''n'' 'D''\0' '6' '1' '2' '-''0''2''-''5' '4''\0' nama noKP tahunMasuk matrik p 'A''1' '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); 19 hari bulan tahun tarikhLahir Masukkan tarikh lahir: _

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); 19 hari bulan tahun tarikhLahir Masukkan tarikh lahir: Tarikh 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 matrik p1

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 matrik p1 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 matrik p1 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 matrik p1 Nama, no KP, matrik, tahun? E Bin F _ 'E'' 'B''i''n'' 'F''\0' '7''1' '0''1''0''-''0''3''-''5''1''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 matrik p1 Nama, no KP, matrik, tahun? E Bin F A23324 _ 'E'' 'B''i''n'' 'F''\0' '7''1' '0''1''0''-''0''3''-''5''1''0' '\0' 'A''2''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 matrik p1 E Bin F A _ 'E'' 'B''i''n'' 'F''\0' '7''1' '0''1''0''-''0''3''-''5''1''0' '\0' 'A''2''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 ??? 8 hari bulan tahun hariIni _

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; 8 hari bulan tahun salinan hari bulan tahun hariIni _