C/C++ 指针谜题?
指针是一个存储另一个变量地址的变量。指针的数据类型与变量的数据类型相同。
在这个谜题中,你需要知道所使用指针的大小。该谜题通过询问你变量的大小来检验你对指针的理解。
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
广告