在面试C/C++相关的岗位时,sizeof操作符几乎是必考内容,它能检验面试者的基本功,所以,只能全对,不能出错。
一起来看看,这些题目,你能全做对吗?说明:如下题目是基于64位平台。
1. sizeof(基本类型)
#include<iostream>
#include<cstring>
using namespace std;
void fun1(char str[20])
{
cout << sizeof(str) << endl;
}
void fun2(char a[10][9])
{
cout << sizeof(a) << endl;
}
int func3()
{
cout << "hello world" << endl;
return 0;
}
int main()
{
cout << sizeof(int) << endl; // 4
cout << sizeof(long) << endl; // 8
int i = 0;
cout << sizeof(++i) << endl; // 4
cout << i << endl; // 0, 注意不是1哦
cout << sizeof(func3()) << endl; // 4, 注意没有hello world打印哈
char *p1;
cout << sizeof(p1) << endl; // 8
int *p2 = new int[100];
cout << sizeof(p2) << endl; // 8
delete [] p2;
float *p3;
cout << sizeof(p3) << endl; // 8
doubl