How does pointer comparison work in C? Is it ok to compare pointers that don't point to the same array?

前端 未结 7 1073
野的像风
野的像风 2020-12-16 10:07

In K&R (The C Programming Language 2nd Edition) chapter 5 I read the following:

First, pointers may be compared under certain circumstances. If

相关标签:
7条回答
  • 2020-12-16 10:26

    Then asked what

    p[0].p0 < p[0].p1
    p[1].p0 < p[1].p1
    p[2].p0 < p[2].p1
    

    Evaluate to. The answer is 0, 1, and 0.

    These questions reduce to:

    1. Is the heap above or below the stack.
    2. Is the heap above or below the string literal section of the program.
    3. same as [1].

    And the answer to all three is "implementation defined". Your prof's questions are bogus; they have based it in traditional unix layout:

    <empty>
    text
    rodata
    rwdata
    bss
    < empty, used for heap >
    ...
    stack
    kernel
    

    but several modern unices (and alternative systems) do not conform to those traditions. Unless they prefaced the question with " as of 1992 "; make sure to give a -1 on the eval.

    0 讨论(0)
提交回复
热议问题