Why pointers can only be subtracted ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abhishek Jha

    #1

    Why pointers can only be subtracted ?

    We cann't add two pointers, We cann't multiply two pointers, We cann't
    divide two pointers, But We can subtract two pointers. Why ?
  • Jonathan Burd

    #2
    Re: Why pointers can only be subtracted ?

    Abhishek Jha wrote:[color=blue]
    > We cann't add two pointers, We cann't multiply two pointers, We cann't
    > divide two pointers, But We can subtract two pointers. Why ?[/color]

    Quick! Grab a good book!
    Q: What does a pointer variable contain?
    A: _______________ _ (fill in the blanks)

    Regards,
    Jonathan.

    --
    Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

    "We must do something. This is something. Therefore, we must do this."
    - Keith Thompson

    Comment

    • infobahn

      #3
      Re: Why pointers can only be subtracted ?

      Abhishek Jha wrote:[color=blue]
      >
      > We cann't add two pointers, We cann't multiply two pointers, We cann't
      > divide two pointers, But We can subtract two pointers. Why ?[/color]

      A pointer gives us a location. Think of it as a street number if
      you like (for the purposes, at least, of this explanation).

      Consider the addresses 1562 Ram Street and 1838 Ram Street.

      If we add these, we get 3400 Ram Street, but what does that mean?
      Nothing, really. There may not even be such an address.

      If we multiply them, we get 2870956 Ram squared Street squared.
      Meaningless.

      If we divide one by the other, we get 0. Again, meaningless.

      But if we subtract one from the other, we get 276 - the number
      of houses you'd have to walk past to get from one to the other
      (including one of the end points). This turns out to be a
      useful number.

      Note that we can usefully do addition of pointers with integers.
      For example, 1562 Ram Street + 276 gives us 1838 Ram Street, a
      useful address.

      Comment

      • Ben Pfaff

        #4
        Re: Why pointers can only be subtracted ?

        infobahn <infobahn@btint ernet.com> writes:
        [color=blue]
        > Consider the addresses 1562 Ram Street and 1838 Ram Street.[/color]

        [...]
        [color=blue]
        > But if we subtract one from the other, we get 276 - the number
        > of houses you'd have to walk past to get from one to the other
        > (including one of the end points). This turns out to be a
        > useful number.[/color]

        You must live in a place with a different approach to addresses.
        Around here, the value you'd get would not be of such
        fine-grained meaning. All you could say is that 1838 Ram Street
        is probably about 4 blocks from 1562 Ram Street (including the
        end blocks).
        --
        "The fact that there is a holy war doesn't mean that one of the sides
        doesn't suck - usually both do..."
        --Alexander Viro

        Comment

        • Richard Tobin

          #5
          Re: Why pointers can only be subtracted ?

          In article <41FE34D6.1ED48 436@btinternet. com>,
          infobahn <infobahn@btint ernet.com> wrote:
          [color=blue]
          >Consider the addresses 1562 Ram Street and 1838 Ram Street.
          >
          >If we add these, we get 3400 Ram Street, but what does that mean?
          >Nothing, really.[/color]

          It's easy to assign reasonable meanings to it. For example, it's
          something that you can divide by two to find the "average" address
          (the midpoint of an array, for example). Or you can alternate between
          the two addresses by subtracting the current one from it.

          It's not that it's meaningless or useless, it's just not useful enough
          to exist in C.

          -- Richard

          Comment

          • infobahn

            #6
            Re: Why pointers can only be subtracted ?

            Ben Pfaff wrote:[color=blue]
            >
            > infobahn <infobahn@btint ernet.com> writes:
            >[color=green]
            > > Consider the addresses 1562 Ram Street and 1838 Ram Street.[/color]
            >
            > [...]
            >[color=green]
            > > But if we subtract one from the other, we get 276 - the number
            > > of houses you'd have to walk past to get from one to the other
            > > (including one of the end points). This turns out to be a
            > > useful number.[/color]
            >
            > You must live in a place with a different approach to addresses.[/color]

            Probably, yes.
            [color=blue]
            > Around here, the value you'd get would not be of such
            > fine-grained meaning. All you could say is that 1838 Ram Street
            > is probably about 4 blocks from 1562 Ram Street (including the
            > end blocks).[/color]

            To stretch the analogy still further, we could argue that as far
            as comp.lang.c is concerned the subtraction is meaningless unless
            the two houses are on the /same/ block. :-)

            Comment

            • Richard Bos

              #7
              Re: Why pointers can only be subtracted ?

              infobahn <infobahn@btint ernet.com> wrote:
              [color=blue]
              > Ben Pfaff wrote:
              >[color=green]
              > > infobahn <infobahn@btint ernet.com> writes:
              > >[color=darkred]
              > > > But if we subtract one from the other, we get 276 - the number
              > > > of houses you'd have to walk past to get from one to the other
              > > > (including one of the end points). This turns out to be a
              > > > useful number.[/color]
              > >
              > > Around here, the value you'd get would not be of such
              > > fine-grained meaning. All you could say is that 1838 Ram Street
              > > is probably about 4 blocks from 1562 Ram Street (including the
              > > end blocks).[/color]
              >
              > To stretch the analogy still further, we could argue that as far
              > as comp.lang.c is concerned the subtraction is meaningless unless
              > the two houses are on the /same/ block. :-)[/color]

              That's not a very far stretch; it is similar to two pointers only being
              usefully subtractable if they point to the same object.

              Richard

              Comment

              • infobahn

                #8
                Re: Why pointers can only be subtracted ?

                Richard Bos wrote:[color=blue]
                >
                > infobahn <infobahn@btint ernet.com> wrote:
                >[color=green]
                > > To stretch the analogy still further, we could argue that as far
                > > as comp.lang.c is concerned the subtraction is meaningless unless
                > > the two houses are on the /same/ block. :-)[/color]
                >
                > That's not a very far stretch; it is similar to two pointers only being
                > usefully subtractable if they point to the same object.[/color]

                Precisely my point.

                Comment

                • Eric Sosman

                  #9
                  Re: Why pointers can only be subtracted ?



                  Richard Tobin wrote:[color=blue]
                  > In article <41FE34D6.1ED48 436@btinternet. com>,
                  > infobahn <infobahn@btint ernet.com> wrote:
                  >
                  >[color=green]
                  >>Consider the addresses 1562 Ram Street and 1838 Ram Street.
                  >>
                  >>If we add these, we get 3400 Ram Street, but what does that mean?
                  >>Nothing, really.[/color]
                  >
                  >
                  > It's easy to assign reasonable meanings to it. For example, it's
                  > something that you can divide by two to find the "average" address
                  > (the midpoint of an array, for example). Or you can alternate between
                  > the two addresses by subtracting the current one from it.[/color]

                  No, there's a sort of "hidden zero" problem in the
                  addition. Let's change analogies to escape the peculiarities
                  of street numbers:

                  What is the sum of "noon today" and "noon tomorrow?"

                  What is the sum of "Acme Building, tenth floor" and
                  "Acme Building, eleventh floor?"

                  What is the sum of the (nominal) freezing and boiling
                  points of water? Does your answer depend on the chosen
                  temperature scale? Why?

                  What is the sum of "middle C" and "concert A?" Does
                  it sound good?

                  Pointers are like numbers that denote positions on a
                  scale of some kind. It makes sense to subtract two such
                  positions to find the interval between them, and it makes
                  sense to add two intervals, or to add a position and an
                  interval. But it doesn't make sense to add two positions;
                  one can only do so by agreeing on a zero reference and then
                  converting one or both positions to intervals.

                  --
                  Eric.Sosman@sun .com

                  Comment

                  • Mike Wahler

                    #10
                    Re: Why pointers can only be subtracted ?

                    "Abhishek Jha" <abhishekjha10@ gmail.com> wrote in message
                    news:88fd7c76.0 501310517.318c5 086@posting.goo gle.com...[color=blue]
                    > We cann't add two pointers,[/color]

                    Why would you want to? What would their sum signify?
                    [color=blue]
                    > We cann't multiply two pointers,[/color]

                    Why would you want to? What would their product signify?
                    [color=blue]
                    > We cann't
                    > divide two pointers,[/color]

                    Why would you want to? What would the quotient signify?
                    [color=blue]
                    >But We can subtract two pointers. Why ?[/color]

                    The difference between two pointers gives the size
                    (in bytes) of the portion of memory whose address
                    begins with the smaller and ends with the larger.

                    Note that subtraction of pointers is only
                    valid if both those pointers point within the same
                    object (or if one or both point one element past the
                    last element of the same array object).

                    Subtraction of other pointers gives undefined
                    behavior.

                    Which C book(s) are you reading?

                    -Mike


                    Comment

                    • Eric Sosman

                      #11
                      Re: Why pointers can only be subtracted ?



                      Mike Wahler wrote:[color=blue]
                      > "Abhishek Jha" <abhishekjha10@ gmail.com> wrote in message
                      > [...][color=green]
                      >>But We can subtract two pointers. Why ?[/color]
                      >
                      > The difference between two pointers gives the size
                      > (in bytes) of the portion of memory whose address
                      > begins with the smaller and ends with the larger.[/color]

                      The difference is in units of the size of the pointed-
                      to type, not necessarily in bytes.

                      AnyType array[2];
                      assert(&array[1] - &array[0] == 1);

                      .... even if `sizeof(AnyType )' is 42, or 97, or 4191.

                      --
                      Eric.Sosman@sun .com

                      Comment

                      • Chris Croughton

                        #12
                        Re: Why pointers can only be subtracted ?

                        On Mon, 31 Jan 2005 06:52:10 -0800, Ben Pfaff
                        <blp@cs.stanfor d.edu> wrote:
                        [color=blue]
                        > infobahn <infobahn@btint ernet.com> writes:
                        >[color=green]
                        >> Consider the addresses 1562 Ram Street and 1838 Ram Street.[/color]
                        >
                        > [...]
                        >[color=green]
                        >> But if we subtract one from the other, we get 276 - the number
                        >> of houses you'd have to walk past to get from one to the other
                        >> (including one of the end points). This turns out to be a
                        >> useful number.[/color]
                        >
                        > You must live in a place with a different approach to addresses.[/color]

                        By the e-address, infobahn is in Britain, where adresses di often work
                        like that (although more of them are word addresses, incrementing by two
                        with odd bytes -- er, houses -- on one side and even ones on the other).
                        [color=blue]
                        > Around here, the value you'd get would not be of such
                        > fine-grained meaning. All you could say is that 1838 Ram Street
                        > is probably about 4 blocks from 1562 Ram Street (including the
                        > end blocks).[/color]

                        Ah, but that's because the pointers refer to different arrays! You have
                        presumably allocated your arrays in less than 100 elements per array...

                        (To continue the analogy, 1532 Ram Street minus 2000 may refer to some
                        house in a totally different street, or possibly a factory or a field,
                        and will result in undefined (and possibly fatal) behaviour if you try
                        calling at that address. In the case of your US blocks of houses, the
                        address 1599 may be between two blocks and you'll get run over by public
                        transport, which might be referred to as a bus error...)

                        Chris C

                        Comment

                        • Richard Tobin

                          #13
                          Re: Why pointers can only be subtracted ?

                          In article <ctm3ql$ntj$1@n ews1brm.Central .Sun.COM>,
                          Eric Sosman <eric.sosman@su n.com> wrote:
                          [color=blue]
                          > Pointers are like numbers that denote positions on a
                          >scale of some kind. It makes sense to subtract two such
                          >positions to find the interval between them, and it makes
                          >sense to add two intervals, or to add a position and an
                          >interval. But it doesn't make sense to add two positions;
                          >one can only do so by agreeing on a zero reference and then
                          >converting one or both positions to intervals.[/color]

                          If I take a tape measure and put the end at some arbitrary distance
                          away, and measure the position of two points (along the line of the
                          measure) with it, then I can find the average position by adding the
                          measurements and dividing by two, and I will get the same answer
                          regardless of where the end of the measure is. Similarly, it works
                          perfectly well to add two temperatures in Fahrenheit and divide by two
                          to get the average, and it also works perfectly well in Celsius and
                          Kelvin, and you get the same answer whichever you use.

                          If a scale has some kind of zero offset, then of course the sum of two
                          values will have two of those offsets in it, but provided the meaning
                          you ascribe to the sum requires you to subtract off a value of the
                          same kind, or divide by two, before interpreting it on the original
                          scale, it will all cancel out.

                          The sum of two pointers doesn't make sense *as a pointer* any more
                          than the sum of two Fahrenheit temperatures makes sense as a
                          Fahrenheit temperature. But that doesn't mean it doesn't make sense
                          at all.

                          In an object-oriented language you could define a type "sum of two
                          pointers", whose constructor takes two pointers. Then you could
                          defined methods on the type such as subtraction of a pointer, which
                          would return a pointer.

                          -- Richard

                          Comment

                          • Peter Nilsson

                            #14
                            Re: Why pointers can only be subtracted ?

                            Richard Tobin wrote:[color=blue]
                            > Eric Sosman wrote:[color=green]
                            > >
                            > > Pointers are like numbers that denote positions on a
                            > > scale of some kind. It makes sense to subtract two such
                            > > positions to find the interval between them, and it makes
                            > > sense to add two intervals, or to add a position and an
                            > > interval. But it doesn't make sense to add two positions;
                            > > one can only do so by agreeing on a zero reference and then
                            > > converting one or both positions to intervals.[/color]
                            >
                            > If I take a tape measure and put the end at some arbitrary distance
                            > away, and measure the position of two points (along the line of the
                            > measure) with it, then I can find the average position by adding the
                            > measurements and dividing by two, and I will get the same answer
                            > regardless of where the end of the measure is. Similarly, it works
                            > perfectly well to add two temperatures in Fahrenheit and divide by[/color]
                            two[color=blue]
                            > to get the average, and it also works perfectly well in Celsius and
                            > Kelvin, and you get the same answer whichever you use.[/color]

                            Temperatures are open ended scales. The sum will remain within infinite
                            range. This may not apply to pointer arithmetic where adding two
                            pointer values need not produce a value within an address space.

                            Also, your average can generally be performed with simple pointer
                            difference...

                            avg = p + (q - p) / 2;

                            If it doesn't, then chances are C99's intptr_t might be useful,
                            if not portable.
                            [color=blue]
                            > If a scale has some kind of zero offset, then of course the sum of[/color]
                            two[color=blue]
                            > values will have two of those offsets in it, but provided the meaning
                            > you ascribe to the sum requires you to subtract off a value of the
                            > same kind, or divide by two, before interpreting it on the original
                            > scale, it will all cancel out.[/color]

                            But C pointers (addresses) need not be 'scale' values.
                            [color=blue]
                            > The sum of two pointers doesn't make sense *as a pointer* any more
                            > than the sum of two Fahrenheit temperatures makes sense as a
                            > Fahrenheit temperature. But that doesn't mean it doesn't make sense
                            > at all.[/color]

                            Indeed, there are multiple possible interpretations that pointer
                            addition _could_ have. But how many of those cannot be implemented
                            using pointer subtraction (either directly or indirectly)?
                            [color=blue]
                            > In an object-oriented language you could define a type "sum of two
                            > pointers", whose constructor takes two pointers. Then you could
                            > defined methods on the type such as subtraction of a pointer, which
                            > would return a pointer.[/color]
                            Do you think you can't do this with C?

                            --
                            Peter

                            Comment

                            • Mike Wahler

                              #15
                              Re: Why pointers can only be subtracted ?


                              "Eric Sosman" <eric.sosman@su n.com> wrote in message
                              news:ctm7rm$pr7 $1@news1brm.Cen tral.Sun.COM...[color=blue]
                              >
                              >
                              > Mike Wahler wrote:[color=green]
                              > > "Abhishek Jha" <abhishekjha10@ gmail.com> wrote in message
                              > > [...][color=darkred]
                              > >>But We can subtract two pointers. Why ?[/color]
                              > >
                              > > The difference between two pointers gives the size
                              > > (in bytes) of the portion of memory whose address
                              > > begins with the smaller and ends with the larger.[/color]
                              >
                              > The difference is in units of the size of the pointed-
                              > to type, not necessarily in bytes.[/color]

                              Are you sure? :-) Remember we're talking about the
                              difference between two pointers, not the result of
                              subtracting an integer value from a pointer.
                              [color=blue]
                              >
                              > AnyType array[2];
                              > assert(&array[1] - &array[0] == 1);
                              >
                              > ... even if `sizeof(AnyType )' is 42, or 97, or 4191.[/color]

                              Did you test that theory? :-)


                              The difference between &array[1] and &array[0] is
                              sizeof(AnyType)

                              -Mike


                              Comment

                              Working...