Home  >  Q&A  >  body text

c++ - 请教c 里面地址的一个基础知识

下面是关于二叉树的代码----pos代表了根据前序遍历pre_str确定了根节点在中序遍历中的位置.

int pos = strchr(in_str, pre_str[0]) - in_str;

我知道指针就是代表地址的一个 unsigned int, 我想问的是这样直接用指针加减来得到的位置为什么不考虑到 sizeof(int), 是说这些指针都是和数据类型的size无关的吗? 有错误的话请大家指正下.

高洛峰高洛峰2714 days ago558

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-17 15:26:45

    The offset is already included in the addition and subtraction of pointers. For example, if the pointer of int* is added to n, its memory address is added to n*sizeof(int). It is not directly added or subtracted according to the memory address.

    reply
    0
  • Cancelreply