ตัวชี้เป็นตัวแปรที่เก็บที่อยู่ของตัวแปรอื่น ชนิดข้อมูลของตัวชี้จะเหมือนกับชนิดข้อมูลที่เป็นตัวแปร
ในปริศนานี้ คุณต้องรู้ขนาดของพอยน์เตอร์ที่กำลังใช้งานอยู่ ปริศนาจะตรวจสอบความเข้าใจของเราเกี่ยวกับพอยน์เตอร์โดยถามคุณถึงขนาดของตัวแปร
ขนาดของ int คือ 4 ไบต์ ในขณะที่ขนาดของตัวชี้ int คือ 8 ตอนนี้ มาทดสอบทักษะของคุณด้วยแบบฝึกหัดต่อไปนี้ในภาษาการเขียนโปรแกรม c++
ตัวอย่าง
#include <iostream>
using namespace std;
int main() {
int a = 6 ;
int *p = &a;
int arr[5][8][3];
int *q = &arr[0][0][0];
int ans;
cout<<"the value of a is "<<a<<endl;
cout<<"predict the size of a ";
cin>> ans;
if(ans == sizeof(p)) {
cout<<"Hurry! your prediction is right";
} else {
cout<<"Your Guess is wrong ";
}
cout<<"Now try this "<<endl;
cout<<"arr is a 3D array"<<endl;
cout<<"predict the size of arr ";
cin>> ans;
if(ans == sizeof(q)) {
cout<<"Hurry! your prediction is right";
} else {
cout<<"Your Guess is wrong ";
}
return 0;
} ผลลัพธ์
the value of a is 6 predict the size of a 8 Hurry! your prediction is right Now try this arr is a 3D array predict the size of arr 4 Your guess is wrong