# PEMROGRAMAN SISTEM POINTER Pertemuan X. ALAMAT & POINTER Pointer sesungguhnya berisi alamat memory yang menyimpan data. Pointer sesungguhnya berisi alamat.

## Presentasi berjudul: "PEMROGRAMAN SISTEM POINTER Pertemuan X. ALAMAT & POINTER Pointer sesungguhnya berisi alamat memory yang menyimpan data. Pointer sesungguhnya berisi alamat."— Transcript presentasi:

PEMROGRAMAN SISTEM POINTER Pertemuan X

ALAMAT & POINTER Pointer sesungguhnya berisi alamat memory yang menyimpan data. Pointer sesungguhnya berisi alamat memory yang menyimpan data. A13 A Variabel BiasaPointer 12 13 12 13

Mengetahui alamat variabel Dengan menambah operator & didepan nama variabel Dengan menambah operator & didepan nama variabel Contoh : Contoh : Int A = 5; cout<<“Variabel A = << &A;

Void main() { int a = 5; int b = 10; Cout<<“ isi variabel A = “<< a<<endl; Cout<<“ isi variabel B = “<< b<<endl; Cout<<“ alamat variabel A = “<< &a<<endl; Cout<<“ alamat variabel B = “<< &b<<endl; }

Variabel pointer Tipe *nama_variabel Contoh : Contoh : Int *a; Float *b; Char *c;

Mengisi variabel Contoh : Contoh : Int a = 55; Int *b; Untuk mengisi : Untuk mengisi : B = &a;

void main() { int a = 5; int b = 10; int *c; int *d; c = &a; d = &b; cout << " alamat a = " << &a << endl; cout << " c = " << c << endl; cout << " alamat b = " << &b << endl; cout << " d = " << d << endl; getch(); }

Mengakses nilai yang ditunjuk void main() { int a = 5; int b = 10; int *c; int *d; c = &a; d = &b; cout << " a = " << a << endl; cout << " c = " << *c << endl; cout << " b = " << b << endl; cout << " d = " << *d << endl; getch(); }

void main() { int a = 5; int b = 10; int *c; int *d; c = &a; d = &b; cout << " a = " << a << endl; cout << " c = " << *c << endl; cout << " b = " << b << endl; cout << " d = " << *d << endl; cout << endl; *c = 15; *d = 20; cout << " a = " << a << endl; cout << " c = " << *c << endl; cout << " b = " << b << endl; cout << " d = " << *d << endl; getch(); }

void main() { void *ptr; int vint = 50; float vfl = 51.5; ptr = &vint; cout << "Nilai yang ditunjuk oleh ptr = " << *(int *)ptr << endl; ptr = &vfl; cout << "Nilai yang ditunjuk oleh ptr : " <<*(float *) ptr << endl; getch(); }

void main() { void *ptr; int vint = 50; ptr = &vint; cout << "Nilai vint = " << vint << endl; *(int *)ptr = 77; cout << "Nilai vint = " << vint << endl; getch(); }

Pointer & Array Int tgl [] = {4, 6, 2008}; Int *ptgl; Untuk menunjuk : Untuk menunjuk : Ptgl = tgl; Bedanya : Bedanya : C = &A Ptgl = &tgl[0];

void main() { void *ptr; int vint = 50; ptr = &vint; cout << "Nilai vint = " << vint << endl; *(int *)ptr = 77; cout << "Nilai vint = " << vint << endl; getch(); }

void main() { int tgl[]= {4, 6, 2008}; int *ptgl; ptgl = tgl; for (int i = 0; i < 3 ; i++) cout << *( ptgl + i ) << endl; getch(); }

void main() { int tgl[]= {4, 6, 2008}; int *ptgl; ptgl = tgl; for (int i = 0; i < 3 ; i++) { cout << *ptgl << endl; cout << ptgl << endl; ptgl++; } getch(); }

Pointer String void main() { char *p = " BAMBANG "; cout << p << endl; getch(); }

void main() { char n[] = "BAMBANG "; char *p = "BAMBANG "; cout << n << endl; cout << p << endl; p++ ; cout << n << endl; cout << p << endl; getch(); }

Array Pointer void main() { char *n[] = {"BAMBANG", "RUDI", "SANTI"}; for(int i = 0; i<3; i++) cout << n[i] << endl; getch(); }

Pointer Menunjuk Pointer Pointer 2 Pointer 1 Variabel X

Contoh : Contoh : Int x; Int *p1; Int **p2; Perintah yang diberikan : P1 = &x; P2 = &p1

void main() { int x = 55; int *p1; int **p2; p1 = &x; p2 = &p1; cout << “ Variabel X = “ << x << endl; cout << “ Variabel p1 = “ << *p1 << endl; cout << “ Variabel p2 = “ << **p2 << endl; getch(); }

Pointer sebagai argumen fungsi Void ubah(int &x); void main() { int x = 10; cout << “ Variabel X = “ << x << endl; ubah(x); cout << “ Variabel X = “ << x << endl; getch(); } Void ubah(int &x) { x = 20; }

void ubah(int *x); void main() { int x = 10; cout << “ Variabel X = “ << x << endl; ubah(&x); cout << “ Variabel X = “ << x << endl; getch(); } void ubah(int *x) { *x = 20; }

Pointer sebagai keluaran fungsi char *bulan(int n); void main () { int bln; cout << “ Bulan = “; cin >> bln; cout << bulan(bln) << endl; } char *bulan(int n) { static char *bulan[] = { “salah”, “Januari”,”Februari”,”Maret”,”April”,”Mei”,“Juni”, ”Juli”,”Agustus”,”September”,”Oktober”,”Nopember”,”Desember” }; return ((n 12 ) ? bulan[0] : bulan [n] ); }

Presentasi serupa