Thread: pointer subtraction

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    pointer subtraction

    hi, all

    I read this programm from one ebook, but i can not under stand how
    (a_ptr - a ) return 5 value.

    Code:
    #include <stdlib.h>
    
    int main()
    {
            int a[]={4,8,2,6,1,0,5,2};
            int *a_ptr;
            a_ptr=a;
            while((*a_ptr)!=0) ++a_ptr; // iterate until 0.
            printf("%d",a_ptr - a); //this gives the resulte 5.
            return 0;
    }
    andybody, help me to understand this.

    Thanks and regards,

    Bhrugu Mehta

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you understand what the while loop does? Try printing *a_ptr. What is the value? To which element of the array does not value correspond? How many elements is it away from the first element of a?
    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
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Do you understand what the while loop does? Try printing *a_ptr. What is the value? To which element of the array does not value correspond? How many elements is it away from the first element of a?
    I analize the while loop and unstand but my question is ,

    "a_ptr" is return address and "a" also return address. than both substract than
    how it retuns 5 value.Actually it returns the substraction value of address...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bhrugu
    "a_ptr" is return address and "a" also return address. than both substract than
    how it retuns 5 value.Actually it returns the substraction value of address...
    The idea here is: if one pointer points to some element in an array, and another pointer points to some element in the same array, then subtracting one pointer from the other results in the number of elements between them.
    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
    Registered User
    Join Date
    May 2010
    Posts
    74
    This only works because array elements are all kept sequentially in memory.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by bhrugu View Post
    I analize the while loop and unstand but my question is ,

    "a_ptr" is return address and "a" also return address. than both substract than
    how it retuns 5 value.Actually it returns the substraction value of address...
    What in your opinion then should it return, if not 5?

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    Quote Originally Posted by itCbitC View Post
    What in your opinion then should it return, if not 5?
    actually it may returns the subtraction of address,

    suppose, a_ptr address 2000
    and a address 1000

    then a_ptr-a=1000

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It might help understanding to realise that the difference between two pointers is related to the size of what those pointers point at.

    In your example, the result of (a_ptr - a) is equivalent to (actual_memory_address_in_a_ptr - actual_memory_address_in_a)/sizeof(int) since both a and a_ptr are treated as pointers to int.

    That is how pointer arithmetic works in C and C++.

    If the pointers are of different types (eg one is a pointer to char, the other a pointer to int) then subtracting them will cause a compiler error, because of a mismatch.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you increment or do pointer arithmetic on a pointer, the pointer is already *sized* to the correct size of the data it's pointing to.

    So in this example, if you change the data type of the array and pointer to type char or type double, the output will still be 5, even though the size of a char is much different than the size of a double.

    Re-read Grumpy's post for a more detailed description of it.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    Quote Originally Posted by grumpy View Post
    It might help understanding to realise that the difference between two pointers is related to the size of what those pointers point at.

    In your example, the result of (a_ptr - a) is equivalent to (actual_memory_address_in_a_ptr - actual_memory_address_in_a)/sizeof(int) since both a and a_ptr are treated as pointers to int.

    That is how pointer arithmetic works in C and C++.

    If the pointers are of different types (eg one is a pointer to char, the other a pointer to int) then subtracting them will cause a compiler error, because of a mismatch.

    ok, thanks, I got your point.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Assuming sizeof(int) is 4, then if subtraction gave a result of 5 then the two pointer addresses differ by 4*5 = 20.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM