首页 > 代码库 > sizeof 用法部分总结

sizeof 用法部分总结

#include<iostream>
#include<string.h>
using namespace std;
struct s1
{
	char a[8];
};

struct s2
{
	double d;
};

struct s3
{
	s1 s;
	char a;
};

struct s4
{
	s2 s;
	char a;
};
struct s5
{
	int i : 8;
	int j : 4;
	int a : 3;
	double b;
};

int main(){
	
	int  *p1;
	cout << sizeof(p1) << endl;
	cout << sizeof(*p1) << endl;
	cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
	char  *p2;
	cout << sizeof(p2) << endl;
	cout << sizeof(*p2) << endl;
	cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
	int a[10];
	char b[] = "hello";
	string s = "hello";
	cout << sizeof(a) << endl;   //
	cout << sizeof(b) << endl;   //计算‘\0’
	cout << strlen(b) << endl;   //不计算‘\0‘
	cout << sizeof(s) << endl;
	cout << s.size() << endl;
	cout << s.capacity()<<endl;
	cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
	cout << sizeof(s1) << endl; // 8
	cout << sizeof(s2) << endl; // 8
	cout << sizeof(s3) << endl; // 9
	cout << sizeof(s4) << endl; // 16;
	cout << sizeof(s5) << endl; // 16;
	system("pause");
	return true;
}

  

sizeof 用法部分总结