Thread: Subtracting Two Pointers

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    Question Subtracting Two Pointers

    Here is a small piece of code.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int    *p, *q;
    
        p = (int *) 1000;
        q = (int *) 2000;
    
        printf("%d", q - p);
    
        return 0;
    }
    When I compile and run it, I get the answer as '500'. Is it because, when you subtract two pointers, you get the number of units of [data type] that lies in between them? (I tried using char. Then the output was '1000'. For float, it was '250', and for double, it was '125'.)

    I have one more question. Why is the type cast (int *) necessary? When I do without the cast, I get a warning: 'Nonportable pointer conversion'. What does that mean? Although the warning comes, the program runs just fine.

    Please help.

    I use Turbo C++ 3.0.

  2. #2
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187

    Re: Subtracting Two Pointers

    >> When I compile and run it, I get the answer as '500'. Is it because, when you subtract two pointers, you get the number of units of [data type] that lies in between them? (I tried using char. Then the output was '1000'. For float, it was '250', and for double, it was '125'.)

    "int" takes 2 bytes so (2000-1000)/2 = 500
    "char" takes 1 byte so (2000-1000)/1 = 1000
    and so on for float (4 bytes) and double (8 bytes)

    >>I have one more question. Why is the type cast (int *) necessary? When I do without the cast, I get a warning: 'Nonportable pointer conversion'.

    *p = 1000; // value pointed to by p is 1000
    p = (int *)1000; //p points to address 1000
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    7
    Hi!

    1. Don't know the answer to your question but:

    P & q are pointers - why?
    If you wanna substract the value of p from q then this is not necessary:

    Code:
    int p, q;
    
    p = 2000;
    q = 1000;
    
    printf("%d",p-q);
    
    ... etc

    The type conversion is requierd because you defined them as pointers. Their data type is int* now, not just int.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    You've answered your first question yourself but this may provide
    more food for thought.

    Consider the following array types:

    Assuming the size of a char on your machine is one byte then,

    char carray[10]; is a declaration of a char array that has a size of
    10 * sizeof(char) bytes. (check sizeof(carray) returns 10 bytes)

    Assuming the size of an int on your machine is two bytes then,

    int iarray[10]; is a declaration of an int array that has a size of
    10 * sizeof(int) bytes. (check sizeof(iarray) returns 20 bytes)

    Assuming the size of a pointer on your machine is four bytes then,

    int* parray[10]; is a declaration of a int* array that has a size of
    10 * sizeof(int*) bytes. (check sizeof(parray) returns 40 bytes)


    As for the second part of the question, you've explicitly declared
    two variables of type int pointer. The expected assignments should
    therefor be addresses (lvalues) that hold (dereference) integers (rvalues).

    You're attempting to assign literal constants to integer pointer variables,
    that is why the cast is necessary.

    Hope that helps.


    This is my first post on the cboard. Hi to everyone.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by c99
    Assuming the size of an int on your machine is two bytes then,...
    Most compilers today are 32bit, so the int is 4-bytes. But for 16bit compilers, 2bytes are correct.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    quzah your back!!!!! Yeah

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by linuxdude
    quzah your back!!!!! Yeah
    I'll second that, the board's been missing Quzah's wit.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    So when does he start being witty?
    R.I.P C89

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by c99
    So when does he start being witty?
    As soon as you start being right for a change.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by c99
    Waltp.

    Really? Tell me something I don't already know!
    ______________

    So when does he start being witty?
    ______________

    Oh! Bite me.
    Obviously we can't. You've been programming for 14 months and have obviously learned more than Hammer, quzah, and myself. In my case, I've probably been programming longer than you've been alive. Not sure about the others, but dang close.

    Lay off the cute stuff. Half your posts have been questionable to belligerent, most of these toward us and we don't deserve it. And all in a short 3 days.

    Grow up and help those that don't know as much as you, as the other half of your posts have done.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Waltp.

    You're being a dick, take it to PM or stfu.
    R.I.P C89

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >take it to PM or stfu.
    Take your own advice please. If your post does not add anything useful to the thread then you should not submit it.

    >You're being a dick
    And what about you? All I've seen so far in your posts is someone who knows enough about C to think he's an expert, but cannot take constructive criticism gracefully when somebody else proves otherwise.
    My best code is written with the delete key.

  13. #13
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Is it legal?

    I don't think the above code is a part of standard C.
    Quoting from the standard 6.5.6.9
    When two pointers are subtracted, both shall point to elements of the same array object,
    or one past the last element of the array object; the result is the difference of the
    subscripts of the two array elements.
    The size of the result is implementation-defined,
    and its type (a signed integer type) is ptrdiff_t defined in the <stddef.h> header.
    If the result is not representable in an object of that type, the behavior is undefined. In other words, if the expressions P and Q point to, respectively, the i-th and j-th elements of an array object, the expression (P)-(Q) has the value i−j provided the value fits in an object of type ptrdiff_t. Moreover, if the expression P points either to an element of an array object or one past the last element of an array object, and the expression Q points to the last element of the same array object, the expression ((Q)+1)-(P) has the same value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and has the value zero if the expression P points one past the last element of the array object, even though the expression (Q)+1 does not point to an element of the array object.
    I don't think the pointers being subtracted satisfy the highlighted condition.
    The one who says it cannot be done should never interrupt the one who is doing it.

  14. #14
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Also the d should be declared as a ptrdiff_t.
    The one who says it cannot be done should never interrupt the one who is doing it.

  15. #15
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Prelude.

    An expert? Heck, I'm a beginner and I know it. Pity you guys dont.
    I've alread stated I've only been programming C for 14 months.
    I'll consider myself an expert after ten years perhaps. Grow up!

    Sheesh!
    R.I.P C89

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM