C/C++ 指针谜题?

cc++server side programmingprogramming更新于 2024/9/25 7:06:00

指针是存储另一个变量地址的变量。指针的数据类型与变量的数据类型相同。

在这个谜题中,您需要知道正在使用的指针的大小。这个谜题通过询问变量的大小来检查我们对指针的理解。

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

相关文章