两个指针相减的值的类型问题
2015-01-30 15:04
686 查看
转载地址:http://www.cnblogs.com/liulipeng/archive/2012/10/08/2715246.html
对于指向同一数组arr[5]中的两个指针之差的验证:
数组如下:ptr = arr;
![](http://1881.img.pp.sohu.com.cn/images/blog/2011/12/6/21/26/u116608527_134d22c5241g215.jpg)
-------------------------------------------------------------------------------------------
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,它是一种有符号整数类型。减法运算的值为两个指针在内存中的距离(以数组元素的长度为单位,而非字节),因为减法运算的结果将除以数组元素类型的长度。所以该结果与数组中存储的元素的类型无关。
ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.同上,使用ptrdiff_t来得到独立于平台的地址差值.
![](https://img-blog.csdn.net/20150130151629484?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWluZ3poZW50YW53bw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
所以如果忘记了ptrdiff 可以用 int 或long int 来代替它做两指针相减后的类型。
类似的还有如下类型:(点击这里)
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t.设计size_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;
![](http://1881.img.pp.sohu.com.cn/images/blog/2011/12/6/21/26/u116608527_134d22c5241g215.jpg)
-------------------------------------------------------------------------------------------
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,它是一种有符号整数类型。减法运算的值为两个指针在内存中的距离(以数组元素的长度为单位,而非字节),因为减法运算的结果将除以数组元素类型的长度。所以该结果与数组中存储的元素的类型无关。
ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.同上,使用ptrdiff_t来得到独立于平台的地址差值.
所以如果忘记了ptrdiff 可以用 int 或long int 来代替它做两指针相减后的类型。
类似的还有如下类型:(点击这里)
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t.设计size_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
}
[/code]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.
相关文章推荐
- C指针加整数、两个指针相减的问题
- C++指针加整数、两个指针相减的问题
- [转]C++指针加整数、两个指针相减的问题
- [转] C++指针加整数、两个指针相减的问题
- 解决两个float类型的数值相减,精确度不准确的问题
- C++指针加整数、两个指针相减的问题
- C++指针加整数、两个指针相减的问题
- 两个二进制数相减的相关问题
- 两个指针有关的问题
- jdbc连oracle数据库发现的两个问题,类型长度大于最大值和不允许进行此操作
- 查找程序中的错误(平凡的程序,不平凡的问题) 4.不定义第三个变量,互换两个变量的值 5.char 类型转换 6.输出 10~100 之间的随机整数
- C#中一个窗体传递另一个窗体的值和sting类型到double类型的转换两个问题
- 指针定义两个字符串,用strcpy交换问题?
- 两个双精度数相减后尾数错误的问题
- win7 局域网文件共享的两个问题——没有权限访问和未授予用户请求登陆类型
- 函数返回类型为指针类型时的一些问题
- 函数的返回类型为指针类型时的若干思考(字符串常量问题)
- 关于const指针和指向const类型的指针的问题
- 数组类型与数组指针的巧妙利用 - 回复 "Delphier" 的问题
- 如何把一个字符串填充到一个无类型的指针 - 回复 "豪杰的爸爸" 的问题