转自:http://www.cnblogs.com/liulipeng/archive/2012/10/08/2715246.html

http://longzxr.blog.sohu.com/196837377.html

对于指向同一数组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_t、ptrdiff_t【转】的更多相关文章

  1. size_t, ptrdiff_t, size_type, difference_type

    size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以负数,std:: ...

  2. c++中,size_typt, size_t, ptrdiff_t 简介

    size_type 类型 从逻辑上来讲,size() 成员函数似乎应该返回整形数值,或如 2.2 节“建议”中所述的无符号整数.但事实上,size 操作返回的是 string::size_type 类 ...

  3. size_type、size_t、differentce_type以及ptrdiff_t

      目录(?)[-] size_type size_t different_type ptrdiff_t size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::siz ...

  4. STL__size_t, ptrdiff_t, size_type, difference_type

    http://blog.csdn.net/zhaowei123191/article/details/5617559 ize_t 是unsigned类型, 用于指明数组长度或下标,它必须是一个正数,s ...

  5. 编程中的offsetof

    linux和windows平台都已经定义了offsetof函数,用于取struct类型中某个变量的偏移量 在stddef.h头文件中,该宏的完整说明如下: #ifdef __cplusplus #if ...

  6. 【C】 06 - 标准库概述

    任何程序都会有一些通用的功能需求,对这些需求的实现组成了库.它可以提高程序的复用性.健壮性和可移植性,这也是模块化设计的体现.C规范定义了一些通用接口库,这里只作概述性介绍,具体细节当然还是要查阅规范 ...

  7. 一些基本的C/C++数据类型

    size_t size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof ...

  8. c++语法集锦

    1.指针的引用 他也是引用,引用是特定内存块的别名 2.变量定义 更准确的说是内存使用约定,并为该约定命名 命名3.指向常变量的指针和常指针 有点拗口,都是指针,但对于所在内存块的使用约定不同.常变量 ...

  9. ANSI C中取得结构体字段偏移量的常用方法

    来自http://blog.chinaunix.net/u2/62910/showart_492571.html 假设在ANSI C程序中定义了一个名为MyStruct的结构类型,其中有一个名为MyF ...

  10. stl空间配置器简介

    1. 符合STL标准的空间配器接口 STL是c++中使用非常广泛的一个标准库,它包含各种有用的容器.而空间配置器作为STL各种容器的背后的核心,负责容器内部内存的分配和释放.不过空间配置器可以分配的也 ...

随机推荐

  1. 分享一个编程学习网站:https://github.com/justjavac/free-programming-books-zh_CN

    分享一个编程学习网站:https://github.com/justjavac/free-programming-books-zh_CN

  2. Applied Nonparametric Statistics-lec9

    Ref:https://onlinecourses.science.psu.edu/stat464/print/book/export/html/12 前面我们考虑的情况是:response是连续的, ...

  3. LeetCode(215) Kth Largest Element in an Array

    题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

  4. UVa 11552 DP Fewest Flops

    题解 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...

  5. JAVA 基础--开发环境 vscode 搭建

    对于使用 Visual Studio Code 的 Java 开发者来说,Language Support for Java(TM) by Red Hat 扩展提供了非常好的语言特性支持,比如智能感知 ...

  6. webdriver高级应用- 测试过程中发生异常或断言失败时进行屏幕截图

    封装了三个类来实现这个功能: 1.DataUtil.py 用于获取当前的日期以及时间,用于生成保存截图文件的目录名,代码如下: #encoding=utf-8 import time from dat ...

  7. Leetcode31--->Next Permutation(数字的下一个排列)

    题目: 给定一个整数,存放在数组中,求出该整数的下一个排列(字典顺序):要求原地置换,且不能分配额外的内存 举例: 1,2,3 → 1,3,2:  3,2,1 → 1,2,3:  1,1,5 → 1, ...

  8. Mongodb 删除记录里的某个字段

    //例如要把User表中address字段删除 db.User.update({},{$unset:{'address':''}},false, true)

  9. Python面向对象(组合、菱形继承、多态)

    今日内容: 1.组合 2.菱形继承 3.多态与多态性 昨天内容重点回顾: 1)调用也叫实例化:发生了2件事  1.创造空对象  2.触发对象下的__init__方法,然后将p连同参数一同传给init  ...

  10. 紫书第一章训练1 D -Message Decoding

    Some message encoding schemes require that an encoded message be sent in two parts. The first part, c ...