Thread: Arithmetic of pointers.

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Arithmetic of pointers.

    Well, I have read about pointers and something still unclear for me in aspect of arithmetic pointers.

    if I have an array[5] type of int then analogy in other word the array is about: array+ steps for progressing into the array, my question if I have the pointer of fifth byte of the array lets assume it's X and I have the pointer of 2nd byte of the array lets assume it as Y, then X-Y will give me the pointer of the " third byte " and not the pointer of the second byte, am I right? it's not giving me the second byte because we are "subtracting" it !

    thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    if I have an array[5] type of int then analogy in other word the array is about: array+ steps for progressing into the array, my question if I have the pointer of fifth byte of the array lets assume it's X and I have the pointer of 2nd byte of the array lets assume it as Y, then X-Y will give me the pointer of the " third byte " and not the pointer of the second byte, am I right? it's not giving me the second byte because we are "subtracting" it !
    I am going to assume that "pointer of fifth byte of the array" is a typo for "pointer to the fifth element of the array", and that the "fifth element of the array" is at index 4. Likewise, "2nd byte" is a typo for "2nd element". You cannot freely use "byte" and "element" interchangeably because an int almost certainly consists of more than one byte.

    When you validly subtract one pointer from another, you get a signed integer result of type ptrdiff_t, i.e., X - Y would give you 3. This means that Y is "3 steps" from X, as in Y points to an element that is 3 elements away from what X points to, and indeed Y points to the element at index 1 and X points to the element at index 4, and 1 + 3 = 4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    i am going to assume that "pointer of fifth byte of the array" is a typo for "pointer to the fifth element of the array", and that the "fifth element of the array" is at index 4. Likewise, "2nd byte" is a typo for "2nd element". You cannot freely use "byte" and "element" interchangeably because an int almost certainly consists of more than one byte.

    When you validly subtract one pointer from another, you get a signed integer result of type ptrdiff_t, i.e., x - y would give you 3. This means that y is "3 steps" from x, as in y points to an element that is 3 elements away from what x points to, and indeed y points to the element at index 1 and x points to the element at index 4, and 1 + 3 = 4.
    much appreciated , wasn't known that subtracting pointers give you number, and adding pointers give you also number .. !

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can't add one pointer to another.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    You can't add one pointer to another.
    Alright, and another quick question, once I write int arr[50] , arr is a pointer to the first element, and in memory there's no byte of arr that's pointing on like int * p= arr means there's a specific byte(or more than one byte) which p is there and pointing to the array ; in other words: the " | |" analogy to array's bytes.

    | p |---------------> arr | |
    | |
    | |
    my question is, is also arr pointing like this way, he has a separately byte for pointing on, and he's just constant differently from p :
    |arr| -----------------> | |
    | |
    | |

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    once I write int arr[50] , arr is a pointer to the first element
    No, arr is an array of 50 int elements. It most contexts, it is converted to a pointer to its first element, but conceptually it is not a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arithmetic
    By Ducky in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2012, 09:45 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. arithmetic help
    By freeindy in forum C Programming
    Replies: 10
    Last Post: 09-18-2007, 08:01 AM
  4. Pointers, arithmetic, and order of operation.
    By Aerie in forum C Programming
    Replies: 4
    Last Post: 04-19-2005, 07:35 AM
  5. Replies: 41
    Last Post: 07-04-2004, 03:23 PM

Tags for this Thread