-3
#include <stdio.h>
#include <stdlib.h>


int main(){
    int i[5] = {0};
    printf("%d - %d = %d" , i+1 , i , (i+1)-(i));
    return 0;
}

I know that an int is 4 bytes but aren't address supposed to work like normal numbers (int)?

Rob
  • 14,746
  • 28
  • 47
  • 65
Saad Jlil
  • 31
  • 4

1 Answers1

0

No, addresses are not supposed to work like normal numbers. The compiler automatically adjusts address arithmetic to work in units of elements, using elements of the type pointed to.

Do not print addresses with %d. Use %p and cast the address to void *.

Do not print differences of addresses with %d. Use %td.

A correct printf would be printf("%p - %p = %td.\n", (void *) (i+1), (void *) i, (i+1) - i);. This will print “1” for the difference because the addresses i+1 and i differ by one element.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312