0% found this document useful (0 votes)
15 views3 pages

Pointer Operators

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Pointer Operators

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ លំហាត់ Pointer Operators

ទ្រនិចចង្អុលរិស ឬ រីតង្
ំ ណាមួយ។
- &(Address-Of): ររួលបាន Memory Address នន អថេរមួយ។
- *(Dereference): Access ថៅកាន់តំនលរបស់ Memory Address។
Example 1: Pointer Operators
C++ Code:
#include<iostream>
using namespace std;

int main(){
//Pointer Operators
//Variable
int a = 20;
//ptr stores the address of a
int *ptr = &a;//Copy Address of a to ptr
cout << "*ptr: " << *ptr << endl;
cout << "ptr: " << ptr << endl;
return 0;
}
Output:

Example 2: Swapping Address


C++ Code:
#include<iostream>
using namespace std;

int main(){
//Pointer Operators
//Variable Declaration
int x, y;
//Read
cout << "---------------------" << endl;
cout << "Read x:";
cin >> x;
cout << "Read y:";
cin >> y;
cout << "---------------------" << endl;
////Pointer Operators
int *ptrx = &x;//Copy Memory Address of x to ptr
int *ptry = &y;//Copy Memory Address of x to ptr

cout << "----------**---------" << endl;


cout << "*ptrx: " << *ptrx << endl;//Value of x
cout << "ptrx: " << ptrx << endl;//Address of x
cout << "*ptry: " << *ptry << endl;//Value of y
cout << "ptry: " << ptry << endl;//Address of y
cout << "----------**---------" << endl;
//Swap the address
int *temp = ptrx;
ptrx = ptry;
ptry = temp;
cout << "----------**---------" << endl;
cout << "*ptrx: " << *ptrx << endl;//Value of x
cout << "ptrx: " << ptrx << endl;//Address of x
cout << "*ptry: " << *ptry << endl;//Value of y
cout << "ptry: " << ptry << endl;//Address of y
cout << "----------**---------" << endl;
return 0;
}
Output:

Example 3: Swapping Address


C++ Code:
#include<iostream>
using namespace std;

int main(){
//Pointer Operators
//Swpping Address
int x, y, z;
x = 10;
y = 20;
z = 30;
//Pointer Operators
int *ptrx = &x;
int *ptry = &y;
int *ptrz = &z;
cout << "-------------------------------" << endl;
cout << "Before Swapping Address:" << endl;
cout << "Value of x: " << *ptrx << endl;//10
cout << "Address of x: " << ptrx << endl;
cout << "Value of y: " << *ptry << endl;//20
cout << "Address of y: " << ptry << endl;
cout << "Value of z: " << *ptrz << endl;//30
cout << "Address of z: " << ptrz << endl;
cout << "-------------------------------" << endl;
//Swapping Address
int *temp = ptrx;
ptrx = ptry;
ptry = ptrz;
ptrz = temp;
cout << "-------------------------------" << endl;
cout << "After Swapping Address:" << endl;
cout << "Value of x: " << *ptrx << endl;//20
cout << "Address of x: " << ptrx << endl;
cout << "Value of y: " << *ptry << endl;//30
cout << "Address of y: " << ptry << endl;
cout << "Value of z: " << *ptrz << endl;//10
cout << "Address of z: " << ptrz << endl;
cout << "-------------------------------" << endl;
return 0;
}
Output:

You might also like