c++ sizeof

指针

char *b = "helloworld";
char *c[10];
double *d;
int **e;


cout << "char *b=" << sizeof(b) << endl;//指针指向字符串,值为4  
cout << "char *b=" << sizeof(*b) << endl; //指针指向字符,值为1  
cout << "double *d=" << sizeof(d) << endl;//指针,值为4  
cout << "double *d=" << sizeof(*d) << endl;//指针指向浮点数,值为8  
cout << "int **e=" << sizeof(e) << endl;//指针指向指针,值为4  
cout << "char *c[10]=" << sizeof(c) << endl;//指针数组,数组c中的元素都为char型指针,值为4×10 = 40 

数组

int a[5]
cout << sizeof(a) << '\n';  //规定了大小 就按照大小来,输出为5×4 = 20

char str1[5] = "hi";  
cout << sizeof(str1) << '\n';  //规定了大小 就按照大小来,输出为5
char str2[] = "hi";    
cout << sizeof(str2) << '\n';  //没有规定大小,就按照字符的个数 但是末尾会有/n,输出3
	
char str3[] = { 'a', 'b', 'c', 'd' }; 
cout << sizeof(str3) << '\n';  //没有规定大小,输出4
char str4[10] = { 'a', 'b', 'c', 'd' };
cout << sizeof(str4) << '\n';  //规定了大小,就按照大小来,输出为10
	
char str5[] = { 'a', 'b', 'c', 'd', '\0', 't',' ','u' };
cout << sizeof(str5) << '\n';  //等于8  中间的'\0'不受影响,' '也不受影响
cout << strlen(str5) << '\n';  //等于4  只计算到'\0'前

数组作为函数参数

32位系统指针的大小为4,64位系统指针的大小为8

void func(char a[3])  
{  
    int c = sizeof(a); //c = 4,因为这里a不在是数组类型,而是指针,相当于char *a。  
}  
  
void funcN(char b[])  
{  
    int cN = sizeof(b); //cN = 4,理由同上。  
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值