Substance:
- Understanding Pointers
- Pointer Operators
- Declaring a Pointer Variable
- Pointer On Pointer
- Pointers In Arrays
- Pointer on String
Pointers In C++
1. Understanding Pointers
A pointer is a pointer variable, containing a value that points to the address of a particular memory location. So a pointer does not contain a data value, but rather a memory address. The memory location can be represented by a variable or also in the form of a direct memory address.
2. Pointer Operators
There are two operators used in the pointer data type, namely:
a. Deference Operator (&)
This operator is usually called the address of or address operator. By using this deference operator (&), a variable will produce a memory address.
Example:
int x = 45;
cout << &x;
In the program above, the memory address of the variable x will be displayed, not the value of x.
b. Reference operator (*)
This operator is usually called value pointed by. By using this operator, we can directly access the value contained in a memory address.
Example:
int x = 45;
cout <<*&x;
In the program above, the value of the memory address &x will be displayed.
3. Declaring Pointer Variables
A pointer variable is defined in the following form:
tipe_data *nama_variabel
- data_type can be any type as in defining non-pointer variables.
- variable_name is the name of the pointer variable.
Example1:
#include <iostream.h>
#include <conio.h>
void main() {
int x, y; int *px;
x = 89;
y = x;
px = &x;
cout << "Nilai x = " << x << endl;
cout << "Nilai y = " << y << endl;
cout << "Alamat px = " << px << endl;
cout << "Nilai px = " << *px << endl;
getch();
}
Output:
Value x = 89
Value y = 89
Address px = 0x0012ff88
Value px = 89
Examples 2:
#include <iostream.h>
#include <conio.h>
void main() {
int x, y;
int *px;
x = 89;
y = x;
px = &x;
cout << "Nilai x = " << x << endl;
cout << "Nilai y = " << y << endl;
cout << "Alamat x = " << &x << endl;
cout << "Alamat px = " << px << endl;
cout << "Nilai px = " << *px << endl;
x = 108;
cout << "\nNilai x = " << x << endl;
cout << "Nilai y = " << y << endl;
cout << "Alamat x = " << &x << endl;
cout << "Alamat px = " << px << endl;
cout << "Nilai px = " << *px << endl;
*px = 123;
cout << "\nNilai x = " << x << endl;
cout << "Nilai y = " << y << endl;
cout << "Alamat x = " << &x << endl;
cout << "Alamat px = " << px << endl;
cout << "Nilai px = " << *px << endl;
getch();
}
Output:
Value x = 89
Value y = 89
Address x = 0x0012ff88
Address px = 0x0012ff88
Value px = 89
Value x = 108
Value y = 89
Address x = 0x0012ff88
Address px = 0x0012ff88
Value px = 108
Value x = 123
Value y = 89
Address x = 0x0012ff88
Address px = 0x0012ff88
Value px = 123
4. Pointer On Pointer
Not limited to pointing to the address of a variable, pointers can also point to other pointers. In the declaration, we add a pointer reference (*) to the variable to be pointed to.
Example:
int x;
int *px; //pointer ke variabel int
**ppx; //pointer pada pointer
x = 100;
px = &nama;
ppx = &pNama;
Example Program:
#include <iostream.h>
#include <conio.h>
void main() {
int x;
int *px; //pointer ke variabel
int **ppx; //pointer ke pointer
x = 175;
px = &x;
ppx = &px;
cout << "Nilai x = " << x << endl;
cout << "Nilai px = " << *px << endl;
cout << "Nilai ppx = " << **ppx << endl;
getch();
}
Output:
Value x = 175
Value px = 175
Value ppx = 0x0012ff88
5. Pointers In Arrays
In an Array, the pointer only needs to show the address of the first element because the array address in memory is arranged sequentially.
Example:
int a[] = {76, 67, 88, 98};
int *pa;
pa = a;
The statement pa=a means that the pointer pa stores the address of array a, which address is represented by the address of the first element, namely a[0]. We can also replace the command pa=a with pa=&a[0]. To read all array elements with a pointer, we can use a loop as in the following program excerpt.
for (int i=0; i < 4; i++) {
cout << *pa << " ";
pa++;
}
Example Program:
#include <iostream.h>
#include <conio.h>
#define MAX 5
void main() {
int a[MAX];
int *pa; pa = a; //atau pa = &a[0]
for (int i = 0; i < MAX; i++) {
cout << "Masukkan Nilai " << i+1 << " : ";
cin >> a[i];
}
cout << endl;
for (int i = 0; i < MAX; i++) {
cout << "Nilai a[" << i << "] = " << *pa << endl;
pa++;
}
getch();
}
Output:
Enter Value 1 : 100
Enter Value 2 : 120
Enter Value 3 : 50
Enter Value 4 : 111
Enter Value 5 : 47
Value of a[0] = 100
Value of a[1] = 120
Value of a[2] = 50
Value of a[3] = 111
Value of a[4] = 47
6. Pointers on Strings
You can see pointers to strings in the following program example:
#include <iostream.h>
#include <conio.h>
#define MAX 5
void main() {
char nama[] = "Albert Einstein";
char *pNama = nama;
cout << "Nama = " << nama << endl;
cout << "pNama = " << pNama << endl;
pNama += 7; cout << "\nSetelah pNama += 7" << endl;
cout << "Nama = " << nama << endl;
cout << "pNama = " << pNama << endl;
getch();
}
Output:
Name = Albert Einstein
pName = Albert Einstein
After pName += 7
Name = Albert Einstein
pName = Einstein
Source:
System Programming Practical Module. STMIK El Rahma Yogyakarta. By Eding Muh. Saprudin, S.Kom
Other Version Notes
C++ language allows us to manipulate memory by using pointers. This is a feature that is not provided by other programming languages. If used correctly it will be very beneficial, but if used incorrectly it can result in damage (crash or hang) to the operating system.
1. Concept and Definition of Pointer
Pointers are variables. But unlike normal variables, pointers store addresses in memory, not the values we enter. Consider the following example.
Example 9.20. Declaring a pointer
#include <iostream>
using namespace std;
int main() { long *Alamat; long X;
Alamat = &X;
X = 5; // Mengisikan nilai 5 ke dalam variabel X
cout<<"Nilai X : "<<X<<endl; cout<<"Nilai *Alamat : "<<*Alamat<<endl; cout<<"Nilai Alamat : "<<Alamat<<endl;
cout<<"Nilai &X : "<<&X<<endl;
*Alamat = 20; // Mengisikan nilai 20 ke dalam *Alamat
cout<<"Nilai X : "<<X<<endl; cout<<"Nilai *Alamat : "<<*Alamat<<endl; cout<<"Nilai Alamat : "<<Alamat<<endl; cout<<"Nilai &X : "<<&X<<endl;
return 0; }
In the example above, we declare the address variable as a pointer by adding a * in front of the variable name. If we do not use the * sign, the variable will function like a normal variable. We declare the X variable as a normal variable with a long data type. Pay attention to the Address = &X line. This line states that the Address variable (not a pointer) will be filled with the value of the address of X. The & sign in front of the variable name means that we want the value of the memory address that we use and not its value. If we execute the program, the display will look like Figure 9.4.
Figure 9.4. Result of executing pointer declaration
Note the output values in Figure 9.4. A value like 0x22ff88 is the hexadecimal number of the variable address. If you observe, when we enter the value 5 in the variable X, the variable *Address will also contain the value 5. Likewise, when we enter the value 20 in the variable *Address, the value of X also changes to 20. This is because the variables *Address and X occupy the same memory address.
Every time we declare a pointer, it automatically points to a random address in memory. Therefore, we must set the pointer variable so that it does not point to a specific address by giving it a NULL value. Consider the following code example and its execution results (Figure 9.5).
Example 9.21. Declaring a pointer with NULL
#include <iostream>
using namespace std;
int main() { long *Alamat; long *Alamat1;
Alamat = NULL;
cout<<"Alamat memori yang ditunjuk dengan NULL :
"<<Alamat<<endl; cout<<"Alamat memori yang ditunjuk tanpa NULL :
"<<Alamat1<<endl;
return 0; }
Figure 9.5. Result of executing a NULL pointer
2. Using New and Delete on Pointers
The New keyword can be used to allocate memory in an empty space. This keyword is followed by the data type to be allocated so that the compiler will know how much memory to allocate. While delete is the opposite of new. Delete will free memory from the variables we use. This delete command is very important, because if we do not free memory from use, it will be very wasteful of memory usage and will eventually make the system run improperly. Consider the following examples of using new and delete.
Example 9.22. Use of new and delete
#include <iostream>
using namespace std;
int main() { int *Alamat;
// Melakukan alokasi memori
Alamat = new int;
// Menggunakan memori yang telah dialokasikan
*Alamat = 100;
cout<<"Nilai *Alamat : "<<*Alamat<<endl;
// membebaskan memori delete Alamat;
return 0; }
Hope this is useful & happy learning!