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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP