1 Pointer wijanarto
2 Topik Introduction to Pointers Pointers dan Parameter Fungsi
3 char ch = ’A’; ’A’ 0x2000 ch: Alamat Memori dr suatu variabel Nilai dari variabel ch. Alamat memori dr variabel ch.
4 Operator & Alamat memori dr suatu obyek dikenal sebagai “address operator.” &ch Shg ch bernilai 0x2000 char ch = ’A’; ’A’ 0x2000
5 “conversion specifier” utk mencetak alamat memori char ch; printf(“%p”, &ch); Contoh:
6 Pointer ch 0x1FFF0x20000x20010x20020x1FFE etc. ‘B’ 0x2000 chPtr 0x3A15 Suatu variabel dapat disimpan dalam alamat memori dari variabel lainnya
7 Pointer Suatu pointer adalah suatu variable. berisi sebuah memory address. Menunjuk ke suatu tipe data. Variabel Pointer biasa dinamakan dg varPtr.
8 cPtr: char* cPtr; Contoh: Kita tulis cPtr sbg pointer ke char. 0x2004 Dpt menyimpan alamat dr variabel bertipe char
9 Pointers dan operator & Contoh: A c: 0x2000 char c = ’A’; char *cPtr; cPtr: 0x2004 cPtr = &c; 0x2000 Assign alamat c ke cPtr.
10 Catatan pada Pointer int* numPtr; float* xPtr; Contoh: Kita dpt memiliki pointer ke data apa saja int *numPtr; float * xPtr; Contoh: Tanda * dapat diletakan diantara tipe dan nama variabel.
11 Catatan pada Pointer Kita dpt mencetak alamat yg disimpan dalam pointer menggunakan konversi %p printf(“%p”, numPtr); Contoh: Kita dpt meng-assign alamat variabel ke pointer yang “compatible” dg menggunakan operator &. intaNumber; int*numPtr; numPtr = & aNumber; Contoh:
12 Catatan pada Pointer int *numPtr; Hati-hati pointer belum di inisialisasikan ??? numPtr
13 Catatan pada Pointer int *numPtr = NULL; NULL numPtr Saat mendeklarasikan pointer, lebih baik selalu meng-inisialisasikannya ke NULL (konstanta pointer khusus ).
14 Operator * Mengijinkan pointer mengakses variabel yang ditunjuknya. Dikenal sbg operator “dereferencing” Jangan bingun dengan * dalam deklarasi pointer
15 A c: 0x2000 B Pointer dan Operator Contoh: char c = ’A’; char *cPtr = NULL; cPtr: 0x2004 cPtr = &c; 0x2000 *cPtr = ’B’; Merubah nilai variabel yg menunjuk ke cPtr
16 Langkah mudah pada Pointer Step 1: Deklarasikan variabel yg ditunjuk. intnum; charch = ‘A’; float x; num: ‘A’ ch: x:
17 Langkah mudah pada Pointer Step 2: Deklarasikan variabel pointer. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr: chPtr: xPtr: num: ‘A’ ch: x:
18 Langkah mudah pada Pointer Step 3: Assign alamat dari variabel ke pointer. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr = # chPtr = &ch; xPtr = &x; numPtr: addr dr num addr dr ch chPtr: addr dr x xPtr: num: ‘A’ ch: x: Tipe Pointer memiliki hub pada tipe var yg ditunjuknya
19 Langkah mudah pada Pointer Step 4: De-reference pointer. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr = # chPtr = &ch; xPtr = &x; *xPtr = 0.25; *numPtr = *chPtr; num: 65 ‘A’ ch: 0.25 x: numPtr: addr dr num addr dr ch chPtr: addr dr x xPtr:
20 Pointer dan Parameter Function Contoh: Fungsi menukar nilai 2 var: x: 1 y: 2 tukar x: 2 y: 1
21 #include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } Bad swap
22 #include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: Bad swap
23 #include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: 1 2 a: b: tmp: Bad swap
24 #include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: 1 2 a: b: 1 tmp: Bad swap
25 #include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: 2 2 a: b: 1 tmp: Bad swap
26 #include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: 2 1 a: b: 1 tmp: Bad swap
27 #include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: 2 1 a: b: 1 tmp: Bad swap
28 #include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } Good swap
29 #include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: Good swap
30 #include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: addr dr x addr dr y a: b: tmp: Good swap
31 #include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } 1 2 x: y: addr dr x addr dr y a: b: 1 tmp: Good swap
32 #include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } 2 2 x: y: addr dr x addr dr y a: b: 1 tmp: Good swap
33 #include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } 2 1 x: y: addr dr x addr dr y a: b: 1 tmp: Good swap
34 #include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } 2 1 x: y: Good swap
35 Pointer dan Argumen Function Merubah nilai dari variabel di actual parameter scanf. char ch; intnumx; float numy; scanf(“%c %d %f”, &ch, &numx, &numy);
Pointer dan Array int *ptr; *ptr = 2; Misal integer 2 byte (ptr) terletak pada alamat memory Bagaimana jika ditulis ptr + 1; berapa alamat memory yang ditunjuk ? 36
Pointer dan Array Misal terdefinisi sbb: int my_array[] = {1,23,17,4,-5,100}; Akses array dengan pointer : int *ptr; ptr = &my_array[0]; 37
Pointer dan Array #include int my_array[] = {1,23,17,4,-5,100}; int *ptr; int main(void) { int i; ptr = &my_array[0]; printf("\n\n"); for (i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); printf("ptr + %d = %d\n",i, *(ptr + i)); } return 0; } 38 printf("ptr + %d = %d\n",i, *ptr++); printf("ptr + %d = %d\n",i, *(++ptr)); RUBAH
Pointer dan String char my_string[40]; my_string[0] = 'T'; my_string[1] = 'e'; my_string[2] = 'd': my_string[3] = '\0'; char my_string[40] = {'T', 'e', 'd', '\0',}; char my_string[40] = "Ted"; 39
Pointer dan String #include char strA[80] = "Demonstrasi sring dengan pointer"; char strB[80]; int main(void) { char *pA; /*pointer ke karakter */ char *pB; puts(strA); /* cetak string A */ pA = strA; /* point pA pada string A */ puts(pA); /* cetak yang di tunjuk pA */ pB = strB; /* pB menunjuk string B */ putchar('\n'); /*ganti baris */ while(*pA != '\0') /* selama masih beum pindah baris) */ { *pB++ = *pA++; } *pB = '\0'; puts(strB); return 0; } 40
Pointer dan Struktur struct tag { char lname[20]; /* nama belakang*/ char fname[20]; /* nama depan */ int age; /* umur*/ float rate; /*12.75/jam */ }; 41
Pointer dan Struktur #include struct tag { char lname[20]; char fname[20]; int age; float rate; }; struct tag my_struct; int main(void) { strcpy(my_struct.lname,"Jensen"); strcpy(my_struct.fname,"Ted"); printf("\n%s ",my_struct.fname); printf("%s\n",my_struct.lname); return 0; } 42
Pointer dan Struktur struct tag *st_ptr; st_ptr = &my_struct; (*st_ptr).age = 63; st_ptr->age = 63; 43
Pointer dan Struktur #include struct tag{ char lname[20]; char fname[20]; int age; float rate; }; struct tag my_struct; void show_name(struct tag *p); int main(void) { struct tag *st_ptr; /* pointer ke structure */ st_ptr = &my_struct; /* pointer menunjuk ke my_struct */ strcpy(my_struct.lname,"Jensen"); strcpy(my_struct.fname,"Ted"); printf("\n%s ",my_struct.fname); printf("%s\n",my_struct.lname); my_struct.age = 63; show_name(st_ptr); /* pass the pointer */ Return 0; } 44 void show_name(struct tag *p) { printf("\n%s ", p->fname); /* p points to a structure */ printf("%s ", p->lname); printf("%d\n", p->age); }
Pointer dan Alokasi Memory Alokasi memori saat running malloc() dan calloc() Mengalokasikan 10 integer int *iptr; iptr = (int *) malloc(10 * sizeof(int)); if (iptr==NULL) /*cek teralokasi/tidak*/ 45
Ekuivalensi Array dan Pointer int *iptr; iptr = (int *) malloc(10 * sizeof(int)); if(iptr==NULL)/*cek teralokasi/tidak*/ … int k; for (k = 0; k < 10; k++) iptr[k] = 2; //set semua ke 2 46
Pointer Dan DeAlokasi Memori free() Membuang/menghapus/mendealokasikan variabel dalam heap int *iptr; iptr = (int *) malloc(10 * sizeof(int)); … free (iptr); 47