1. ## 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 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. Originally Posted by RyanC
if I have an array 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. 3. Originally Posted by laserlight 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. You can't add one pointer to another. 5. Originally Posted by laserlight You can't add one pointer to another.
Alright, and another quick question, once I write int arr , 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. Originally Posted by RyanC
once I write int arr , 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. Popular pages Recent additions array, assume, byte, pointer, pointers 