c++高手解答疑惑

来源:百度知道 编辑:UC知道 时间:2024/07/05 03:17:34
怎么样用运算符sizeof测量两个变量a和b的字节数?

cout<<sizeof(a)<<endl;

#include<iostream.h>
void main()
{
int i;
char j;
char a[20]="nihao";
cout<<"i="<<sizeof(i)<<endl;
cout<<"j="<<sizeof(j)<<endl;
cout<<a="<<sizeof(a)/sizeof(char)<<endl;
}
要注意:在a的数组只能在静态和外部中才能算出。

Example

// Example of the sizeof keyword
size_t i = sizeof( int );

struct align_depends {
char c;
int i;
};
size_t size = sizeof(align_depends); // The value of size depends on
// the value set with /Zp or
// #pragma pack

int array[] = { 1, 2, 3, 4, 5 }; // sizeof( array ) is 20
// sizeof( array[0] ) is 4
size_t sizearr = // Count of items in array
sizeof( array )