015
C Pointers
Tim Dosen DTE
PDP2015
Mengapa mempelajari
Pointer?
Memory address
Setiap variabel yang kita deklarasikan akan
mereservasi suatu lokasi dalam memori
komputer
Lokasi tsb memiliki address
Untuk mengakses address memori tsb, gunakan
tanda & (ampersand)
Contoh:
scanf(%d, &var1);
Code
#include <stdio.h>
Result
Address of var1 variable: 6a33e9ac
Address of var2 variable: 6a33e9a0
int main ()
{
int var1;
char var2[10];
printf("Address of var1 variable: %x\n", &var1 );
printf("Address of var2 variable: %x\n", &var2 );
return 0;
}
Algoritma&Pemrog
Ayah
Citra
Sedang
kos
Citra
Jalan Baru
no 10
Bayu ingin memberi kado pada Citra
(tapi tidak tau alamatnya). Maka dapat
dilakukan dengan cara:
langsung memberikannya pada Citra
(direct)
menitipkannya ke Ayah Citra (indirect)
Bayu
Algoritma&Pemrog
Algoritma&Pemrog
Pointer Declaration
Tanda* digunakan untuk mendeklarasikan
pointer
<variable_type> *<pointer_var_name>;
Algoritma&Pemrog
Pointer Initialization
Untuk mendapatkan address dari variable
yang ingin ditunjuk oleh pointer, gunakan
tanda & (address operator)
int y = 5;
int *yPtr;
yPtr = &y;
yPtr menunjuk ke y
Dengan cara ini berarti kita sudah
melakukan inisialisasi pada pointer
Algoritma&Pemrog
10
Algoritma&Pemrog
11
#include <stdio.h>
int main ()
{
int
y = 20;
int
*yPtr;
yPtr = &y;
);
Algoritma&Pemrog
12
Indirection
Untuk mengakses value secara indirect, gunakan
tanda * (indirection/dereferencing operator)
Contoh sebelumnya:
printf("Value of *yPtr variable: %d\n", *yPtr );
Menghasilkan:
Value of *yPtr variable: 20
dan
lain
&
/* changes y to 7 */
Algoritma&Pemrog
13
Outline
fig07_04.c
(1 of 2 )
Algoritma&Pemrog
14
NULL Pointers
Ada kalanya kita tidak/belum ingin
meng-assign sebuah pointer ke suatu
address variable
Disebut Null Pointer
Meng-assign NULL value ke pointer
Dilakukan saat variable declaration.
15
#include <stdio.h>
int main ()
{ int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
Algoritma&Pemrog
16
*number
Algoritma&Pemrog
17
Outline
fig07_06.c
Call by Value
Algoritma&Pemrog
18
Outline
Function prototype takes a pointer argument
fig07_07.c
Call by Reference
Function cubeByReference is
passed an address, which can be the
value of a pointer variable
Algoritma&Pemrog
19
Algoritma&Pemrog
Fig. 7.8 | Analysis of a typical call-by-value.
20
Pointer to Array
Sebenarnya sebuah nama array merupakan
constant pointer pada elemen pertama dari array
tersebut.
Contoh:
double balance[50];
21
*(p + 3) : 17.0
int main ()
*(p + 4) : 50.0
{
Array values using balance as address
/* an array with 5 elements */
*(balance + 0) : 1000.0
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
*(balance + 1) : 2.0
double *p;
*(balance + 2) : 3.4
int i;
*(balance + 3) : 17.0
p = balance;
*(balance + 4) : 50.0
/* output each array element's value */
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ )
{
printf("*(p + %d) : %.1f\n",
i, *(p + i) );
}
printf( "Array values using balance as address\n");
for ( i = 0; i < 5; i++ )
{
printf("*(balance + %d) : %.1f\n",
i, *(balance + i) );
}
return 0;
Algoritma&Pemrog
}
22
Contoh berikutnya:
convertToUppercase( string ); /*string adalah array. Tanpa
tanda & */
Algoritma&Pemrog
23
Outline
fig07_10.c
(1 of 2 )
Algoritma&Pemrog
24
Outline
fig07_10.c
(2 of 2 )
Both sPtr and *sPtr are modified by the
convertToUppercase function
Algoritma&Pemrog
25
Algoritma&Pemrograman2
015
C Structure
PDP2014
26
What is Structure in C?
Array dapat menyimpan beberapa
(banyak) data dengan jenis yang sama
Structure dapat menyimpan beberapa
data dengan tipe yang berbeda-beda.
Namun data yang berbeda-beda
tersebut mempunyai hubungan satu
sama lain.
Structure dapat merepresentasikan
sebuah record
Algoritma&Pemrog
27
Contoh record
Sebuah record dari buku yang disimpan
di perpustakaan akan memiliki atribut
berikut:
Judul
Pengarang
Tema
ID Buku
Algoritma&Pemrog
28
Defining a Structure
Menggunakan struct statement.
struct [Tag_name]
{ type var_member1;
type var_member2;
type var_member n;
};
29
Contoh
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
Tips: Gunakan huruf besar pada huruf pertama pada
nama Structure Tag_name, untuk membedakannya
dengan variabel biasa
Algoritma&Pemrog
30
<Tag_name>
<record_name>
Contoh:
struct Books Book1; // record Book1 of type Book
struct Books Book2; // record Book1 of type Book
Algoritma&Pemrog
31
Contoh:
Book1.book_id = 6495407;
Algoritma&Pemrog
32
#include <stdio.h>
/* book 2 specification */
#include <string.h>
struct Books
Book2.book_id = 6495700;
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
{
/* book 1 specification */
Algoritma&Pemrog
return 0;
}
33
Algoritma&Pemrog
34
Structures as Function
Arguments
functionName
(struct
Tag_name
local_var)
Algoritma&Pemrog
35
#include <stdio.h>
#include <string.h>
Book1.book_id = 6495407;
struct Books
/* book 2 specification */
char author[50];
char subject[100];
Book2.book_id = 6495700;
int book_id;
};
/* function declaration */
void printBook( struct Books book );
return 0;
}
int main( )
{
/* book 1 specification */
36
Pointers to Structures
Cara mendeklarasikan pointer ke sebuah
structure sama dengan membuat pointer ke
variabel biasa
struct
<Tag_name>
*<pointer_name>;
Contoh
struct
Books
Algoritma&Pemrog
*book
37
Contoh
book = &Book1
Contoh
book->title
Algoritma&Pemrog
38
#include <stdio.h>
#include <string.h>
Book1.book_id = 6495407;
struct Books
/* book 2 specification */
char author[50];
char subject[100];
Book2.book_id = 6495700;
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
return 0;
}
int main( )
{
/* book 1 specification */
39
Typedef
Membuat nama baru sebagai tipe data
Sebenarnya hanya membuat alias dari
sebuah tipe data
Digunakan untuk mempersingkat
penulisan
Contoh:
typedef unsigned char byte;
byte b1, b2;
Algoritma&Pemrog
40
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;
Algoritma&Pemrog
41
#include <stdio.h>
#include <string.h>
Book1.book_id = 6495407;
/* book 2 specification */
char author[50];
char subject[100];
Book2.book_id = 6495700;
int book_id;
} Book;
/* function declaration */
void printBook( struct Books *book );
return 0;
}
int main( )
{
/* book 1 specification */
42