Thread: Where does the points point?

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    4

    Where does the points point?

    Hey, I'm struggling to understand why pointer 1 and 3 has their respective positions. This is the code:
    Where does the points point?-pointpointsat-png

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  3. #3
    Registered User
    Join Date
    May 2022
    Posts
    4
    Quote Originally Posted by Salem View Post
    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.
    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?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  5. #5
    Registered User
    Join Date
    May 2022
    Posts
    4
    Quote Originally Posted by Salem View Post
    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-- ?
    After you put it that way I googled it, now I know the difference. Thanks! (the first returns the value after decrement and the second returns the value before decrement)

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    My policy: In case of doubt, DRAW!

    Where does the points point?-imagem-bitmap-png

  7. #7
    Registered User
    Join Date
    May 2022
    Posts
    4
    Quote Originally Posted by flp1969 View Post
    My policy: In case of doubt, DRAW!

    Where does the points point?-imagem-bitmap-png
    It was this part I was struggling with, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-23-2020, 12:38 PM
  2. 8 points in a circle around target point
    By Rick19468 in forum C Programming
    Replies: 68
    Last Post: 10-13-2012, 09:28 PM
  3. C++ Plot Simple Points / Graph, X & Y array points
    By Khadafi in forum C++ Programming
    Replies: 9
    Last Post: 11-11-2011, 03:47 AM
  4. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM

Tags for this Thread