Thread: subtracting one pointer from another

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    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[1024];
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,149
    Quote 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.)

    Quote 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.
    Last edited by laserlight; 06-29-2019 at 06:10 AM.
    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
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Have a look at the ptrdiff_t type

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    533
    Quote Originally Posted by Fauveboy View Post
    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.

    Quote Originally Posted by Fauveboy View Post
    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[2], *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 subscribe to a feed

Similar Threads

  1. Subtracting a macro from a pointer
    By limp in forum C Programming
    Replies: 2
    Last Post: 05-24-2011, 06:38 AM
  2. Subtracting from strings?
    By Xfacter in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2006, 12:01 AM
  3. Subtracting Two Pointers
    By z0diac in forum C Programming
    Replies: 28
    Last Post: 02-26-2004, 10:55 PM
  4. Adding and subtracting.
    By erikcn in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2002, 05:01 AM

Tags for this Thread