说说size_t 和 ptrdiff_t
2013-04-13 11:14
190 查看
来源博文:size_t、ptrdiff_t
对于指向同一数组arr[5]中的两个指针之差的验证:
数组如下:ptr = arr;
-------------------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
char arr[5] = {1,2,3,4,5};
char *ptr = arr;
printf("%d\n",&ptr[4]-&ptr[0]);
system("PAUSE");
return 0;
}
-------------------------------------------------------------------------------------------
运行输出:4
更换为字符数组,测试结果一样。
《C和指针》P110 分析如下:两个指针相减的结果的类型为ptrdiff_t,它是一种有符号整数类型。减法运算的值为两个指针在内存中的距离(以数组元素的长度为单位,而非字节),因为减法运算的结果将除以数组元素类型的长度。所以该结果与数组中存储的元素的类型无关。
类似的还有如下类型:(点击这里)
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t.设计size_t就是为了适应多个平台,其引入增强了程序在不同平台上的可移植性。
ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.同上,使用ptrdiff_t来得到独立于平台的地址差值.
size_type是unsigned类型,表示容器中元素长度或者下标,vector<int>::size_type i = 0;
difference_type是signed类型,表示迭代器差距,vector<int>:: difference_type = iter1-iter2.
前二者位于标准类库std内,后二者专为STL对象所拥有。
//=====================================================================================
http://blog.csdn.net/yyyzlf/article/details/6209935
C and C++ define a special type for pointer arithmetic, namely ptrdiff_t, which is a typedef of a platform-specific signed integral type. You can use a variable of type ptrdiff_t to store the result of subtracting and
adding pointers.For example:
What are the advantages of using ptrdiff_t? First, the name ptrdiff_t is self-documenting and helps the reader understand that the variable is used in pointer arithmetic exclusively. Secondly, ptrdiff_t is portable: its underlying type may vary across platforms,
but you don't need to make changes in the code when porting it.
对于指向同一数组arr[5]中的两个指针之差的验证:
数组如下:ptr = arr;
-------------------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
char arr[5] = {1,2,3,4,5};
char *ptr = arr;
printf("%d\n",&ptr[4]-&ptr[0]);
system("PAUSE");
return 0;
}
-------------------------------------------------------------------------------------------
运行输出:4
更换为字符数组,测试结果一样。
《C和指针》P110 分析如下:两个指针相减的结果的类型为ptrdiff_t,它是一种有符号整数类型。减法运算的值为两个指针在内存中的距离(以数组元素的长度为单位,而非字节),因为减法运算的结果将除以数组元素类型的长度。所以该结果与数组中存储的元素的类型无关。
类似的还有如下类型:(点击这里)
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t.设计size_t就是为了适应多个平台,其引入增强了程序在不同平台上的可移植性。
ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.同上,使用ptrdiff_t来得到独立于平台的地址差值.
size_type是unsigned类型,表示容器中元素长度或者下标,vector<int>::size_type i = 0;
difference_type是signed类型,表示迭代器差距,vector<int>:: difference_type = iter1-iter2.
前二者位于标准类库std内,后二者专为STL对象所拥有。
//=====================================================================================
http://blog.csdn.net/yyyzlf/article/details/6209935
C and C++ define a special type for pointer arithmetic, namely ptrdiff_t, which is a typedef of a platform-specific signed integral type. You can use a variable of type ptrdiff_t to store the result of subtracting and
adding pointers.For example:
#include <stdlib.h> int main() { int buff[4]; ptrdiff_t diff = (&buff[3]) - buff; // diff = 3 diff = buff -(&buff[3]); // -3 }
What are the advantages of using ptrdiff_t? First, the name ptrdiff_t is self-documenting and helps the reader understand that the variable is used in pointer arithmetic exclusively. Secondly, ptrdiff_t is portable: its underlying type may vary across platforms,
but you don't need to make changes in the code when porting it.
相关文章推荐
- size_type、size_t、differentce_type以及ptrdiff_t
- size_t 、wchar_t和 ptrdiff_t
- size_t和ptrdiff_t
- size_type、size_t、different_type以及ptrdiff_t
- size_t & ptrdiff_t
- size_type,size_t和ptrdiff_t
- ptrdiff_t 和 size_t
- c++中,size_typt, size_t, ptrdiff_t 简介
- size_type、size_t、different_type以及ptrdiff_t
- size_type、size_t、differentce_type以及ptrdiff_t
- 关于size_t, ptrdiff_t, size_type, difference_type
- size_type、size_t、different_type以及ptrdiff_t
- size_t、ptrdiff_t
- size_t与ptrdiff_t
- C语言内建数据类型size_t 和 ptrdiff_t
- STL__size_t, ptrdiff_t, size_type, difference_type
- size_t/ptrdiff_t/intptr_t/uintptr_t
- size_t、ptrdiff_t
- size_type、size_t、different_type以及ptrdiff_t
- 关于size_t, ptrdiff_t, size_type, difference_type