关于sizeof运算符的文档

在vs2013关于sizeof的文档中有

When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof.

当sizeof运算符作用在一个char型对象时,返回1.当它作用在一个数组中时,返回这个数组所占的所有bytes数,并不是返回这个代表这个数组的指针的大小。想要获得代表数组指针的大小,需要将其作参数转入一个使用sizeof的函数。

size_t getPtrSize( char *ptr )
{
   return sizeof( ptr );
}

此时,当sizeof接受到char*类型时,返回的是指针的大小。

而直接传入数组名时,得到的则是整个数组所占的大小。

其中大小都是以char的大小为单位。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值