减去两个指针(数组),(C语言)

时间:2019-02-17 13:41:16

标签: c arrays pointers

int vector[] = { 28, 41, 7 };
int *p0 = vector;
int *p1 = vector + 1;
int *p2 = vector + 2;

我知道结果

printf("%p, %p, %p\n", p0, p1, p2);

是ex)100, 104, 108

但为什么是

的结果
printf("p2-p0: %d\n", p2 - p0);
printf("p2-p1: %d\n", p2 - p1);
printf("p0-p1: %d\n", p0 - p1);

2, 1, -1

不是8, 4, -4 ????????

1 个答案:

答案 0 :(得分:0)

当减去指向相同差值而不是地址差值的指针(相同类型,否则没有意义)时:

   <interactive>:520:20: error:
• Couldn't match type ‘Int’ with ‘[Char]’
  Expected type: String
    Actual type: Int
• In the expression: show n
  In an equation for ‘Prelude.show’: Prelude.show (Val n) = show n
  In the instance declaration for ‘Show Expr’

<interactive>:520:25: error:
• Couldn't match expected type ‘Ghci41.Expr’ with actual type ‘Int’
• In the first argument of ‘show’, namely ‘n’
  In the expression: show n
  In an equation for ‘Prelude.show’: Prelude.show (Val n) = show n

<interactive>:521:24: error:
• Couldn't match expected type ‘[Char]’ with actual type ‘Int’
• In the first argument of ‘(++)’, namely ‘brak l’
  In the expression: brak l ++ show o ++ brak r
  In an equation for ‘Prelude.show’:
      Prelude.show (App o l r)
        = brak l ++ show o ++ brak r
        where
            brak (Val n) = show n
            brak e = "(" ++ show e ++ ")"

<interactive>:521:34: error:
• Couldn't match expected type ‘[Char]’ with actual type ‘Int’
• In the first argument of ‘(++)’, namely ‘show o’
  In the second argument of ‘(++)’, namely ‘show o ++ brak r’
  In the expression: brak l ++ show o ++ brak r

<interactive>:521:39: error:
• Couldn't match expected type ‘Ghci41.Expr’ with actual type ‘Op’
• In the first argument of ‘show’, namely ‘o’
  In the first argument of ‘(++)’, namely ‘show o’
  In the second argument of ‘(++)’, namely ‘show o ++ brak r’

<interactive>:521:44: error:
• Couldn't match expected type ‘[Char]’ with actual type ‘Int’
• In the second argument of ‘(++)’, namely ‘brak r’
  In the second argument of ‘(++)’, namely ‘show o ++ brak r’
  In the expression: brak l ++ show o ++ brak r

<interactive>:522:34: error:
• Couldn't match expected type ‘Ghci41.Expr’ with actual type ‘Int’
• In the first argument of ‘show’, namely ‘n’
  In the expression: show n
  In an equation for ‘brak’: brak (Val n) = show n

<interactive>:523:23: error:
• Couldn't match expected type ‘Int’ with actual type ‘[Char]’
• In the expression: "(" ++ show e ++ ")"
  In an equation for ‘brak’: brak e = "(" ++ show e ++ ")"
  In an equation for ‘Prelude.show’:
      Prelude.show (App o l r)
        = brak l ++ show o ++ brak r
        where
            brak (Val n) = show n
            brak e = "(" ++ show e ++ ")"

<interactive>:523:30: error:
• Couldn't match expected type ‘[Char]’ with actual type ‘Int’
• In the first argument of ‘(++)’, namely ‘show e’
  In the second argument of ‘(++)’, namely ‘show e ++ ")"’
  In the expression: "(" ++ show e ++ ")"

<interactive>:523:35: error:
• Couldn't match expected type ‘Ghci41.Expr’
              with actual type ‘Expr’
  NB: ‘Expr’ is defined at <interactive>:518:1-38
      ‘Ghci41.Expr’ is defined at <interactive>:246:1-35
• In the first argument of ‘show’, namely ‘e’
  In the first argument of ‘(++)’, namely ‘show e’
  In the second argument of ‘(++)’, namely ‘show e ++ ")"’

进行type * p1 = ...; type * p2 = ...; (p1 - p2) == (((char *) p1) - ((char *) p2)) / sizeof(type) 时,给出的元素等级为 n 而不是vector + n,这是相同的。所以

((char *) vector) + n