Subtracting two pointers giving unexpected result

后端 未结 6 1243
灰色年华
灰色年华 2021-01-26 14:56
#include 

int main() {
    int *p = 100;
    int *q = 92;
    printf(\"%d\\n\", p - q);  //prints 2
}

Shouldn\'t the output of above pr

6条回答
  •  青春惊慌失措
    2021-01-26 15:19

    According to the standard (N1570)

    When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.

提交回复
热议问题