Basic Cpp: C/C++ Pointer

Pointer menyimpan address dari variable atau memory allocation

// General syntax
datatype *var_name;

// An example pointer "ptr" that holds
// address of an integer variable or holds
// address of a memory whose value(s) can
// be accessed as integer values through "ptr"
int *ptr;

Pointer di C:

1. GunakanĀ unary & (ampersand) untuk mendapatkan address dari variabel. Contoh: &x akan memperoleh address dari variabel x.

2. Gunakan unary * (Asterisk) untuk 2 hal:

a. deklarasi pointer variable

b. untuk mengakses nilai yang disimpan di address.

// C program to demonstrate use of * for pointers in C 
#include <stdio.h>

int main() 
{ 
// A normal integer variable 
int Var = 10;

// A pointer variable that holds address of var. 
int *ptr = &Var;

// This line prints value at address stored in ptr. 
// Value stored is value of variable "var" 
printf("Value of Var = %d\n", *ptr);

// The output of this line may be different in different 
// runs even on same machine. 
printf("Address of Var = %p\n", ptr);

// We can also use ptr as lvalue (Left hand 
// side of assignment) 
*ptr = 20; // Value at address is now 20

// This prints 20 
printf("After doing *ptr = 20, *ptr is %d\n", *ptr);

return 0; 
}

output:

Value of Var = 10
Address of Var = 0x7fffa057dd4
After doing *ptr = 20, *ptr is 20

Pointer Expression dan Pointer Arithmetic

Contoh:

// C++ program to illustrate Pointer Arithmetic 
// in C/C++ 
#include <bits/stdc++.h>

// Driver program 
int main() 
{ 
// Declare an array 
int v[3] = {10, 100, 200};

// Declare pointer variable 
int *ptr;

// Assign the address of v[0] to ptr 
ptr = v;

for (int i = 0; i < 3; i++) 
{ 
printf("Value of *ptr = %d\n", *ptr); 
printf("Value of ptr = %p\n\n", ptr);

// Increment pointer ptr by 1 
ptr++; 
} 
}
Output:Value of *ptr = 10
Value of ptr = 0x7ffcae30c710

Value of *ptr = 100
Value of ptr = 0x7ffcae30c714

Value of *ptr = 200
Value of ptr = 0x7ffcae30c718

Nama Array adalah Pointer

nama array seperti pointer constant, nilai pada pointer constant adalah address dari element pertama.

val dan &val[0] dapat dipertukarkan

// C++ program to illustrate Array Name as Pointers in C++ 
#include <bits/stdc++.h> 
using namespace std;

void geeks() 
{ 
// Declare an array 
int val[3] = { 5, 10, 15};

// Declare pointer variable 
int *ptr;

// Assign address of val[0] to ptr. 
// We can use ptr=&val[0];(both are same) 
ptr = val ; 
cout << "Elements of the array are: "; 
cout << ptr[0] << " " << ptr[1] << " " << ptr[2];

return; 
}

// Driver program 
int main() 
{ 
geeks(); 
return 0; 
}
Output:
Elements of the array are: 5 10 15

Pointer dan Multidimensional Arrays

sintaknya:

nums[i][j] is equivalent to *(*(nums+i)+j)

Contoh:

Pointer Notation Array Notation Value
*(*nums) nums[0][0] 16
*(*nums + 1) nums[0][1] 18
*(*nums + 2) nums[0][2] 20
*(*(nums + 1)) nums[1][0] 25
*(*(nums + 1) + 1) nums[1][1] 26
*(*(nums + 1) + 2) nums[1][2] 27

Bersambung…

Leave a Reply

Your email address will not be published. Required fields are marked *