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