-5

I'm learning pointers basics. Please take a look at my sample code and tell me what is going on.

void main()
{
     int i, *j;
     i = 2;
     j = i;
     printf("%d", j);
     printf("\n%d", j + 1);
     printf("\n%d", j + 2);
}

My output is

2 6 10

Please Explain me..

Filipe Gonçalves
  • 19,404
  • 6
  • 42
  • 65
Prakash Bala
  • 215
  • 4
  • 12

1 Answers1

5

The output you see is because you assigned the address 0x02 to the pointer j when you write j + 1 you are incrementing it by 1 is the same as incrementing it's address by sizeof(int) or sizeof(*j) which is the same.

But the behavior is actually undefined as was commented here originally by @Filipe Gonçalves, and replied to my question - here by @Kninnug

C11 §6.5.6/8:

[..] 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. [..]

you can check the official document to read more about it.

Also, your printf() calls are cause of undefined behavior. You should print pointers with the %p specifier and cast the pointer to void *, and if you want the assignment to be correct you should also cast the address to int * in order for it to work.

Casting the i to (int *) might also be insufficient if your system is 64 bits.

The line

j = i;

could cause undefined behavior if you ever dereference j.

You should not assign an integer to a pointer that way, perhaps you meant

j = &i;

otherwise with compilation warnings enabled, the compiler should warn you about the incorrect assignment, though it's valid it's not correct.


Note: main() returns int, and you have declared/defined it as not returning a value.

Community
  • 1
  • 1
Iharob Al Asimi
  • 51,091
  • 5
  • 53
  • 91
  • `j+1` and `j+2` are UB. Pointer arithmetic is well defined only when the resulting pointer refers an element in the same array, or one position past the end. And printing `j` using the `%d` modifier is also UB. – Filipe Gonçalves Sep 19 '15 at 12:36
  • @FilipeGonçalves Are you sure?, can you please tell me in which section of the standard is that mentioned so I can fix the answer? I was thinking that it was invalid but then it came to my mind that not necessarily, unless you dereference the pointer. – Iharob Al Asimi Sep 19 '15 at 12:45
  • 2
    @iharob C11§6.5.6/8: [..] *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.* [..] – Kninnug Sep 19 '15 at 12:50
  • @Kninnug thank you, I just referenced your comment so please don't remove it. – Iharob Al Asimi Sep 19 '15 at 12:56
  • @iharob no problem, but you might as well include the comment (verbatim or altered: the quoted fragment doesn't explain the whole story) in the answer itself. – Kninnug Sep 19 '15 at 13:01
  • @Kninnug I don't you whether you seen [this question](http://stackoverflow.com/questions/32537471/is-array-1-1-safe-to-use-to-get-the-last-element-of-an-automatic/) or not but there was a discussion on same paragraph . Still some people were not convinced . – ameyCU Sep 19 '15 at 13:05
  • Yes it makes it very clear indeed, since there is no dereference in this code but according to the quote it's still udefined behavior. – Iharob Al Asimi Sep 19 '15 at 13:05
  • Yeah sorry for the delay, @Kninnug hit it. That's exactly what I was talking about. Now that you fixed your answer I can upvote! :) – Filipe Gonçalves Sep 19 '15 at 13:11