Why does pointer arithmetic work between int* & int* only for minus and not plus?

0

I can't seem to understand why I am getting this error. It seems bizarre to me that subtraction works just fine (producing -3) while addition gives me an error.

Why is this the case?, Shouldn't addition return (0x00000001 + 0x00000002 = 0x00000003)?

#include <stdio.h>

int main()
{
    int* x = NULL;
    int* y = NULL;

    // error: invalid operands to binary + (have ‘int *’ and ‘int *’)
    int z = &x[0] + &x[3];

    // but this works fine
    int z = &x[0] - &x[3];

    printf("0x%08x\n", x);
    printf("0x%08x\n", &x[0]);
    printf("%d", z);

    return 0;
}
c
pointers
asked on Stack Overflow Dec 13, 2017 by AlanSTACK • edited Dec 13, 2017 by mrflash818

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0