Pointer is a variable that contains the memory address of a
other variables.
other variables.
This address is the location of other objects (usually other variables) in
memory.
memory.
For example, if a variable contains the address of another variable, the first variable
said pointing to the second variable
said pointing to the second variable
Pointer operators there are two, namely:
OperatorØ &
& Operator is unary (requires only one operand only).
Operator & produces the address of the operandnya.
Operator *Ø
* Are unary operators (requires only one operand only).
Operator * value that is at an address.
pointer declaration
Tipe_data * nama_pointer;
sample program using C + + pointers
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
class node {
public :
int data;
node*berikut;
};
void main(){
//langkah satu
node*baru;
baru=new node;
baru->data=5;
baru->berikut=NULL;
cout<<"Isi data node baru adalah : "<data<
//langkah dua
node*lain;
lain=new node;
lain->data=6;
lain->berikut=NULL;
cout<<"Isi data node lain adalah : "<data<
//langkah tiga : menyambung rantai
baru->berikut=lain;
cout<<"Isi data node lain dicetak dari node baru adalah :";
cout<berikut->data<
//langkah empat
node*kepala=baru;
cout<<"mencetak node pertama dari pointer kepala :";
cout<data<
cout<<"Mencetak node kedua dari pointer kepala :";
cout<berikut->data<
//langkah lima : pointer yang jalan-jalan
cout<<"Menggunakan perulangan untuk mencetak setiap data pada rantai\n";
node*jalan=kepala;
int i = 1;
while(jalan !=NULL){
cout<<"Data ke-"<<<">"<data<
i++;
jalan=jalan->berikut;
}
//langkah enam : bukti bahwa pointer data tidak kehilangan kepala
cout<<"Mencetak node pertama dari pointer kepala : ";
cout<data<
cout<<"Mencetak node kedua dari pointer kepala : ";
cout<berikut->data<
}
system("PAUSE");
return EXIT_SUCCESS;
}
Tidak ada komentar:
Posting Komentar