Data Structures Class 5 Pointer McGraw-Hill Technology Education Copyright 2006 by The McGraw-Hill Companies, Inc. All rights reserved.
What is a variable? 1. Each variable must be defined before you can use it inside your program. 2. Did you ask yourself why we have to declare variables? 1. First reason is... 2. Second reason is...
Definition A variable has a name, an address, a type and a value. A pointer is a variable which declared A pointer is a variable which declared as a pointer type. It contains the memory address (locations) of another variable.
Example For example: if one variable is a data type and second variable is the pointer type which points to the first variable, then the contents of second variable is the address of first variable. Value at address 5 2000 2000 3000 Address of First variable
DECLARATION OF POINTERS Syntax:- int *b;
Similarly we can declare char and floating type pointer:- char *p; float *q;
INITIALIZATION OF POINTER Pointer variable can be initialize by following these steps: 1. first declare or initialized an integer variable (suppose int x;) 2. declare a pointer variable 3. assign address of variable to pointer variable.
syntax int a; int *b; b=&a;
PROGRAM void main(void) { int a=5; int *b; b=&a; printf( value of a =%d,a); printf( value of a =%d,*b); printf( address of a =%u,b); printf( value of b=address of a =%u,b); }
Pointer Operators 10
Pointer Operators (Cont..) 11
DEREFERENCING OPERATION OF POINTER The contents of any variable is referenced by a pointer variable. For example: if int x=5; int *b; b=&x; Now if we want to print the value of x variable by using pointer b. how can it possible? *b performs the "dereferencing" operation on b; it looks at the address stored in b, and goes to that address and returns the value.
bagaimana cara merepresentasikan sebuah diagram pohon dari perusahaan XYZ? Masalah : Kita ingin merepresentasikan struktur data nonlinear ke dalam memori komputer yang bersifat linear. Solusi : pemetaan dengan menggunakan pointer. 13
Terdapat dua cara dalam mengakses huruf c sebagai data yang tersimpan. 1. secara langsung 2. secara tidak langsung dengan menggunakan pointer (dikenal dengan indirection). indirection bertingkat dapat juga diciptakan dengan menggunakan pointer 14
Kelemahan penggunaan pointer: 1. Bisa sangat rumit dan sulit dipakai dengan level indirection yang banyak. 2. Ketika dipakai secara tidak benar, pointer dapat membuat struktur data sangat sulit dimengerti. Kapanpun pointer digunakan dalam membuat struktur data, harus dipertimbangkan tradeoff antara kompleksitas dan fleksibilitasnya. Pointer dan indirection tidak hanya ada pada memori komputer, tapi juga dalam aspek lain pemakaian komputer. e.g. Contohnya hyperlinks pada web page (dapat juga bersifat double indirection ). 15
Pointer vs Variabel Biasa 16
Ilustrasi Pointer. 17
Contoh Program 18
Operator Pointer 19
Contoh Pointer dideklarasikan dengan cara: tipe_data *nama_variabel_pointer; Contoh inisialisasi pointer, contoh: 20
Contoh yang salah 21
Operasi pada Pointer 1. Operasi Assignment Antar variabel pointer dapat dilakukan operasi assignment. Contoh 1: Assignment dan sebuah alamat dapat ditunjuk oleh lebih dari satu pointer Contoh 2: Mengisi variabel dengan nilai yang ditunjuk oleh sebuah variabel pointer Contoh 3: Mengoperasikan isi variabel dengan menyebut alamatnya dengan pointer Contoh 4: Mengisi dan mengganti variabel yang ditunjuk oleh pointer 22
Contoh 1 : Assigment, sebuah alamat dapat ditunjuk oleh lebih dari satu pointer 23
Contoh 2 : Mengisi variabel dengan nilai yang ditunjuk oleh sebuah variabel pointer 24
Contoh 3 : Mengoperasikan isi variabel dengan menyebut alamatnya dengan pointer 25
Contoh 4 : Mengisi dan mengganti variabel yang ditunjuk oleh pointer 26
Operasi pada Pointer (2) 2. Operasi aritmatika Arithmetic operation can also be done with pointer variables. Postfix, prefix, increment, decrement are possible with pointers. 27
FOR EXAMPLE: Example: int a=5; int *b; b=&a; Here b++ or ++b means 1000+2=1002 & b or b means 1000-2=998. Its doesn t affect the address of a. a b 5 1000 1000 2000 a 5 After b++ or ++b b 1002 In other variables Ex- int a=5; a++ or ++a means 5+1=6 and a or a means 5-1=4. 1000 2000