sizeof 用法部分总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 用法部分总结的主要内容,如果未能解决你的问题,请参考以下文章