关于sizeof运算符的文档

来源:互联网 发布:影楼制作软件apk 编辑:程序博客网 时间:2024/05/18 00:56

在vs2013关于sizeof的文档中有

When the sizeof operator is applied to an object of typechar, 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 usessizeof.

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

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

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

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

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

原创粉丝点击