Thread: Subtracting Two Pointers

  1. #16
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    pinko_liberal.

    Ah! I note you've quoted straight from ISO/IEC9899 or C99.

    Yes, it appears you are right, the condition is not satisfied.

    A couple of points to note though, firstly, I doubt the condition
    was ever intended to be satisfied, and secondly and most
    importantly the code portion initally quoted is more likely an
    exercise designed to highlight the sizeof difference between data types.

    However, using pointer arithematic on an array shall meet all
    those conditions from the section you quoted.
    R.I.P C89

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One thing you'll both want to note, is that the origional poster said nothing about arrays. Simply pointer arithmetic.

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

  3. #18
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    quzah,
    You can subtract two pointers only if they point to the elements in the same array object or at most one past the end of the array.
    Other pointer subtractions are meaningless.
    This is my reading of the standard, I might be wrong but I would like to know why.
    regards,
    Pinko
    The one who says it cannot be done should never interrupt the one who is doing it.

  4. #19
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by quzah
    One thing you'll both want to note, is that the origional poster said nothing about arrays. Simply pointer arithmetic.

    Quzah.
    And one thing you should note quzah is that I did in fact note that.

    Another dick that doesn't read entire posts before he opens his mouth.

    Sheesh! You'll try anything for a bite wont you, you dick.
    R.I.P C89

  5. #20
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by c99
    And one thing you should note quzah is that I did in fact note that.

    Another dick that doesn't read entire posts before he opens his mouth.

    Sheesh! You'll try anything for a bite wont you, you dick.
    quzah is a very respected member of this board, please do not use such language against him
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #21
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    pinky.

    Respect is earned, not given!

    Language? What language? I have never at any time used
    language that was not used by mods or other members.
    R.I.P C89

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Respect is earned, not given!
    quzah has earned his. You haven't. And you probably won't in the forseeable future judging from your childish attitude.

    >I have never at any time used language that was not used by mods or other members
    Mods and other members that you've harrassed have been around longer than you.
    My best code is written with the delete key.

  8. #23
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by Prelude

    quzah has earned his. You haven't.
    He may have earnt your respect but not mine. Deal with it.
    Stop being such a cry baby.

    Mods and other members that you've harrassed have been around longer than you.
    Read the above over and over again until you realise the stupidity
    of what you wrote.

    I have not at any point initiated harrassment against anyone. Check your facts dick.
    R.I.P C89

  9. #24
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>He may have earnt your respect but not mine. Deal with it.
    >>Stop being such a cry baby.
    Don't mess with Prelude. She will hurt you.

    >>I have not at any point initiated harrassment against anyone. Check your facts dick.
    "Read the above over and over again until you realise the stupidityof what you wrote."
    Take your own advice.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #25
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Threads_Locked_Because_Of_c99++;

  11. #26
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Pfft. Write more efficient, and more easily read code:

    ++threadsLockedBecauseOfC99;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by pinko_liberal
    quzah,
    You can subtract two pointers only if they point to the elements in the same array object or at most one past the end of the array.
    Other pointer subtractions are meaningless.
    This is my reading of the standard, I might be wrong but I would like to know why.
    regards,
    Pinko
    I stand correct. Other pointer subtractions are undefined.

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

  13. #28
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bah! Doing a pre-incrament produces the same code and a post-incrament in this case.
    For example outputting the following two samples produces the same asm code using gcc with the -S parameter
    Code:
    int main(void)
    {
      int x=5;
      x++;
      return 0;
    }
    And
    Code:
    int main(void)
    {
      int x=5;
      ++x;
      return 0;
    }
    Produces:
    Code:
            .file   "test1.c"
            .version        "01.01"
    gcc2_compiled.:
    .text
            .align 4
    .globl main
            .type    main,@function
    main:
            pushl %ebp
            movl %esp,%ebp
            subl $24,%esp
            movl $5,-4(%ebp)
            incl -4(%ebp)
            xorl %eax,%eax
            jmp .L2
            .p2align 4,,7
    .L2:
            leave
            ret
    .Lfe1:
            .size    main,.Lfe1-main
            .ident  "GCC: (GNU) 2.95.4 20011002 (Debian prerelease)"
    Ok technically its not the same code since the file name will be different but the code is still the same

  14. #29
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    too much trolling to delete....thread closed. If anyone wants to brng this problem back up please use a new thread.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

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