ทดสอบความเข้าใจเกี่ยวกับขนาดของ pointer และ array ในระบบ 64-bit
โค้ดทดสอบ
int a = 6;
int *p = &a;
int arr[5][8][3];
int *q = &arr[0][0][0];
// sizeof(p) = 8 bytes (pointer size บน 64-bit)
// sizeof(q) = 8 bytes (pointer to first element)
// sizeof(arr) = 5*8*3*4 = 480 bytes (ขนาดทั้ง array)
ผลลัพธ์ตัวอย่าง
predict size of a: 8 → Hurry! (sizeof pointer = 8) predict size of arr: 4 → Wrong! (sizeof array = 480, not pointer)
สรุป
- Pointer size = 8 bytes (64-bit) หรือ 4 bytes (32-bit)
- Array size = จำนวนสมาชิก × ขนาดชนิดข้อมูล
- Array name decay to pointer เฉพาะในบางบริบท (เช่น ส่งเข้า function)