Pointer ใช้เก็บที่อยู่ของตัวแปร ในการประกาศตัวแปรพอยน์เตอร์ใน C/C++ ใช้ asterisk (*) นำหน้าชื่อตัวแปร
การประกาศ
*pointer_name
ใน C
ตัวอย่าง
#include <stdio.h> int main() { int a = 7; int *p = &a; printf("Value of Variable: %d\n", *p); printf("Address of Variable: %p\n", p); *p = 6; printf("Value of the variable is now: %d\n", *p); return 0; }ผลลัพธ์
Value of Variable: 7 Address of Variable: 0x6ffe34 Value of the variable is now: 6
ใน C++
ตัวอย่าง
#include <iostream> using namespace std; int main() { int a = 7; int *p = &a; cout<<"Value of Variable: "<<*p<<endl; cout<<"Address of Variable: "<<p<<endl; *p = 6; cout<<"Value of the variable is now: "<<*p<<endl; return 0; }ผลลัพธ์
Value of Variable: 7 Address of Variable: 0x6ffe34 Value of the variable is now: 6