sizeof()几乎在所有笔试中都会出现,我说的是C/C++……
但是各自的考点和考法都不一样
下面讲讲一种少见的考法
在windows NT下32位的C++程序,请计算sizeof的值??
void Func1(char str[100]) //与参数的大小无关
{
cout<<"str Func str:"<<sizeof(str)<<endl;
}
void Func2(char str) //与参数的大小无关
{
cout<<"char Func str:"<<sizeof(str)<<endl;
}
char str[]= "Hello";
char *p = str;
char func_str1[100];
char func_str2;
int n = 10;
void *q = malloc( 100 );
请写出它们的值:
cout<<"str:"<<sizeof(str)<<endl; _________
cout<<"p:"<<sizeof(p)<<endl; _________
cout<<"int:"<<sizeof(n)<<endl; _________
cout<<"malloc q:"<<sizeof(n)<<endl; _________
Func1(func_str1); _________
Func2(func_str2); _________
//计算已经赋值的数据,它的大小就是这个数据类型的大小
//计算形参的大小,也是这个形参数据类型的大小
//不多说
//不直接讲解,看代码,看注释理解吧!代码才是最具说服力的!
#include<iostream>
using namespace std;
void Func1(char str[100]) //与参数的大小无关
{
cout<<"str Func str:"<<sizeof(str)<<endl; //str[] 大小和指针的大小 一样 4字节
}
void Func2(char str) //与参数的大小无关
{
cout<<"char Func str:"<<sizeof(str)<<endl; //就是 char 的大小
}
int main()
{
//不同数据类型的内存大小,与其赋值无关
char str[]= "Hello";
char *p = str;
char func_str1[100];
char func_str2;
int n = 10;
void *q = malloc( 100 );
cout<<"str len:"<<strlen(str)<<endl;
cout<<"str:"<<sizeof(str)<<endl; //长度 + 1(还有一个\0)
cout<<"p:"<<sizeof(p)<<endl; //指针
cout<<"int:"<<sizeof(n)<<endl; //int
cout<<"malloc q:"<<sizeof(n)<<endl;//无论开辟多大看空间,都是一个指针的大小
Func1(func_str1);
Func2(func_str2);
return 0;
}