Hey, I'm struggling to understand why pointer 1 and 3 has their respective positions. This is the code:
Ptr1 points at 1 and ptr3 points at 2 in before, but I dont understand why. It dosen't make sense to me, anybode know why?
Thanks
Hey, I'm struggling to understand why pointer 1 and 3 has their respective positions. This is the code:
Ptr1 points at 1 and ptr3 points at 2 in before, but I dont understand why. It dosen't make sense to me, anybode know why?
Thanks
1. It helps if you post actual code (you know, text from a text editor) rather than a picture.
2. It helps if you have an actual program, with a main(), that results in a runnable program.
3. With an actual runnable program, you can run it in the debugger and step line by line to really see what's happening.
> Ptr1 points at 1 and ptr3 points at 2 in before, but I dont understand why.
Well for ptr1, the [1] and the -- effectively cancel out, so you end up back as if you'd done ptr1 = arr;
ptr3 takes the OLD value of ptr1, before it is decremented with --
So it ends up still pointing to the 2nd element of arr.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Hey, this is the actual code. It's a question from an old exam where we're suppose to draw arrows between the pointers and where they point at!
If ptr3 takes the old value of ptr1 shouldn't it point at the first element in the array then? I don't understand how it points to the second element in the array?
It's an actual picture.
Actual code is something I can copy/paste/compile/test in 4 keystrokes.
Actual code is something you could run in a debugger to answer your own questions.
> If ptr3 takes the old value of ptr1 shouldn't it point at the first element in the array then?
Are you confused by 'old' and 'new'?
Do you know the difference between --foo and foo-- ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
My policy: In case of doubt, DRAW!