To elaborate on the existing answers, I would like to add an explanation.
In your code, i
is an int
variable. You assign the address of i
to pointer
. Fine. Then, what you do is, increment the pointer (address) and then, attempt to dereference it.
Now, in comparison to the statement in your code,
printf("%i\n", *(pointer + 1));
quoting the C11
standard, chapter §6.5.6, Additive operators
[....] If both the pointer
operand and the result point to elements of the same array object, or one past the last
element of the array object, the evaluation shall not produce an overflow; otherwise, the
behavior is undefined. If the result points one past the last element of the array object, it
shall not be used as the operand of a unary *
operator that is evaluated.
Essentially, by doing this, you're trying to access some memory which is not allocated to your process, thereby invoking undefined behavior.
The output of UB, is, well, undefined.