// 比较 strlen(str)和 sizeof(str)的不同 2

#include <STDIO.H>
#include "string.h"


int main()
{
int x = 1122867;  // 0x112233 == 1122867
char str[10] ; 
// strlen()以'\0',作为结束标志,故strlen(str)不确定
printf("strlen of str is: %d \n",strlen(str));


//而sizeof(str)==10
printf("sizeof of str is: %d \n",sizeof(str));


printf("x is: %d\n",x); //x == 1122867


//反汇编的时候发现,strcpy()只给"www.it315.o11"
//分配了12个字节的空间,也许是用str做标准来分配的
//所以,"www.it315.o11"最后的那个"1"会占用,变量x的
//空间,此时0x112233变为,0x112231
strcpy(str,"www.it315.o11"); 


//但是,x != 0x112231,为什么呢,因为,strcpy()把'\0'
//也复制了过来 x == 0x110031 == 1114161
printf("x is: %d\n",x);


//strlen()以'\0',作为结束标志(不包括'\0'),故strlen(str)==13
printf("strlen of str is: %d \n",strlen(str));


// sizeof()函数返回的是变量声明后所占的内存数,不是实际长度 
// 故sizeof(str)==10
printf("sizeof of str is: %d \n",sizeof(str));


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值