# Thread: subtracting one pointer from another

1. ## subtracting one pointer from another

Im confused by the line pos = ptr - buffer;

This is used to find the distance from the start of the buffer to the max samp. But as buffer is always essentially the address of the first element, this makes it zero, correct?

So why not use just pos = ptr; ? This in fact makes a compiling error saying something about not being happy to assign a pointer to an unsigned int.

So what is it about the calculation that makes the unsigned int accept a value from two pointers (one being subtracted by the other?)

Code:
```double = buffer;
double* ptr = buffer + 1024;
double maxval = 0.0;
unsigned long pos = 0;

while(--ptr >= buffer) {
if(*ptr >= maxval){
maxval = *ptr;
pos = ptr - buffer;
}
}

printf("the max sample is %f, at pos %d\n",
maxval, pos);

}``` 2. Originally Posted by Fauveboy
But as buffer is always essentially the address of the first element, this makes it zero, correct?
That would be true if ptr is pointing to the first element (as in index 0) of buffer (since you would then effectively have buffer - buffer, which is 0), but that may not be the case. Hence, pos could be zero, or it could be greater than zero but not more than the number of elements in the buffer. (Well, it could be outside this range, but that would imply that ptr is invalid and not one-past-the-end.) Originally Posted by Fauveboy
So what is it about the calculation that makes the unsigned int accept a value from two pointers (one being subtracted by the other?)
You answered your question yourself: the difference allows you to compute the "distance" between the elements that the respective pointers point to. 3. Have a look at the ptrdiff_t type 4. Originally Posted by Fauveboy So why not use just pos = ptr; ? This in fact makes a compiling error saying something about not being happy to assign a pointer to an unsigned int.
The pointer size is different from the size of data pointed. For example, a pointer to char points to a single byte, but the size of the pointer is bigger. If your processor can deal with 64 bits addresses this will be the size of any pointer... And, in most systems, unsigned int is 32 bits long, so the result could not fit (hence the warning).

The safest types to use to hold calculations with pointers are size_t and ssize_t - but, to make sure, stdint.h also implements intptr_t and uintptr_t. Originally Posted by Fauveboy So what is it about the calculation that makes the unsigned int accept a value from two pointers (one being subtracted by the other?)
As you may know, pointers holds memory addresses. And, in C, arithmetic using pointers takes in account the type pointed. In your example, buffer is allocated somewhere in memory and ptr points somewhere inside this array (ok, initially, ourside, but still a memory address) . Subtracting both pointers will get you the difference (in bytes) from both addresses, but since the type pointed is double, this difference will be divided by sizeof(double).

This is a little different adding or subtracting an integer to or from a pointer... In that case, the offset will be multiplied by the size pointed... Example:
Code:
`int a, *p = a + 1;`
Here p will hold the address of a plus sizeof(int)*1.
In the exemple above, doing:
Code:
`size_t pos = p - a;`
Uses 2 pointers. This is the same as:
Code:
`size_t pos = ((void *)p - (void *)a) / sizeof(int);`
With pointer calculations, void * and char * are equivalent. Popular pages Recent additions buffer, maxval, pos, ptr, unsigned 