#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
typedef union
{
char i[4];
int j[5];//4*5
short int k;
}U;// union类型变量的成员共用同一块内存空间,空间大小是由占用内存空间最大的变量决定的,并且每个成员的首地址是一样的
typedef struct
{
U a;
char b[5];
}S;
void f(int x[100],int y[])
{
int z[100];
printf("%d,%d,%d",sizeof(x),sizeof(y),sizeof(z));//4,4,400,x:只是传递过去a的首地址
}
int main()
{
S size;
char *p="hello";
char p1[]="hello";
int a[100];
int b[10];
f(a,b);
cout<<sizeof(*p)<<endl;//1,*p相当于字符串中的‘h’,所以长度为1
cout<<sizeof(p+1)<<endl;//4,因为所有的指针都是存放地址值 而地址值都是int类型的 所以sizeof(p)==4
cout<<sizeof(p1)<<endl;//6
cout<<sizeof(U)<<endl;//20,共用体的大小
cout<<sizeof(size)<<endl;//20+4+4=28,字节对齐
// cout<<strlen(*p)<<endl;//error
cout<<strlen(p)<<endl;//5
cout<<strlen(p1)<<endl;//5,C风格字符串只要只要是使用双引号的,都会以'\0',就是说会多出一个字符。
return 0;
}
//
16位:short 1byte,int 2byte,word 1byte
32位:short 2byte,int 4byte,word 2byte
sizeof strlen
最新推荐文章于 2024-06-25 21:05:21 发布