This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

subtraction of pointers, debugger mishandling

If I have some simple code involving pointer differences, like the following, -

void main()
{
    int datum;
    int *ptr = &datum;
    int *ptr1;
    int diff;
    ptr1 = ptr + 1;
    diff = ptr1 - ptr;
}

after execution of the last line the CCS debugger will show that diff = 1,
but will erroneously evaluate the expression "ptr1 - ptr" as 4. I conclude that the debugger does not do pointer subtraction correctly though the compiler does.I am using CCS 5 but I think it has always been this way.

  • Robert,

    The compiler knows that ptr and ptr1 are of type (int *), but the debugger only uses the actual values of the pointers and does not interpret those values the same as the compiler does.

    You should be able to use casting in the debugger to make it interpret these values the way you wish.

    If you were to instead write your last code line as

    diff = ptr1;
    diff -= ptr;

    then you would likely get the value 4 in diff. The difference is due to the point at which type conversion is done when moving from (int *) for ptr and ptr1 to (int) for diff.

    The debugger is simply doing this type conversion differently than the compiler. Try type casting to see if you can get the result you want, if this display is important to you.

    Regards,
    RandyP