Valid operations on pointers in C

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

    #1

    Valid operations on pointers in C

    Why division/mulitiplication/addition of pointers are not
    allowed in C?
  • Joona I Palaste

    #2
    Re: Valid operations on pointers in C

    aruna <arunal2001@yah oo.co.in> scribbled the following:[color=blue]
    > Why division/mulitiplication/addition of pointers are not
    > allowed in C?[/color]

    First define how it would work, and then we'll see why it should or
    should not be allowed.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The obvious mathematical breakthrough would be development of an easy way to
    factor large prime numbers."
    - Bill Gates

    Comment

    • Alex

      #3
      Re: Valid operations on pointers in C

      "aruna" <arunal2001@yah oo.co.in> wrote in message
      news:a2efcaed.0 404230628.18b0d 5e1@posting.goo gle.com...[color=blue]
      > Why division/mulitiplication/addition of pointers are not
      > allowed in C?[/color]

      Those operations have no obvious meaning, so there is no need.


      Comment

      • Lew Pitcher

        #4
        Re: Valid operations on pointers in C

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        aruna wrote:

        | Why division/mulitiplication/addition of pointers are not
        | allowed in C?

        A pointer can be considered to be the 'location' of a data item.

        Subtraction of one pointer from another gives you the 'distance' between
        two data items.

        What would addition of two pointers give you? (Hint: What does the
        addition of '12 Oak Lane' to '757 Main Street West' give you?)

        What would multiplication of two pointers give you? (Hint: What does the
        multiplication of '12 Oak Lane' with '757 Main Street West' give you?)

        What would division of two pointers give you? (Hint: What does the
        division of '12 Oak Lane' by '757 Main Street West' give you?)

        While pointer subtraction is meaningful, the other operations are not.

        - --

        Lew Pitcher, IT Consultant, Enterprise Application Architecture
        Enterprise Technology Solutions, TD Bank Financial Group

        (Opinions expressed here are my own, not my employer's)
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.4 (MingW32)

        iD8DBQFAiSyFagV FX4UWr64RAo1XAK C8YTdVBF0bWGf93 dNiVVzWrhMhsQCe OszA
        RKCqbpu4N77+X+8 EE5VcwEk=
        =+jwy
        -----END PGP SIGNATURE-----

        Comment

        • Thomas Matthews

          #5
          Re: Valid operations on pointers in C

          Lew Pitcher wrote:
          [color=blue]
          > -----BEGIN PGP SIGNED MESSAGE-----
          > Hash: SHA1
          >
          > aruna wrote:
          >
          > | Why division/mulitiplication/addition of pointers are not
          > | allowed in C?
          >
          > A pointer can be considered to be the 'location' of a data item.
          >
          > Subtraction of one pointer from another gives you the 'distance' between
          > two data items.
          >
          > What would addition of two pointers give you? (Hint: What does the
          > addition of '12 Oak Lane' to '757 Main Street West' give you?)
          >
          > What would multiplication of two pointers give you? (Hint: What does the
          > multiplication of '12 Oak Lane' with '757 Main Street West' give you?)
          >
          > What would division of two pointers give you? (Hint: What does the
          > division of '12 Oak Lane' by '757 Main Street West' give you?)
          >
          > While pointer subtraction is meaningful, the other operations are not.
          >
          > - --
          >
          > Lew Pitcher, IT Consultant, Enterprise Application Architecture
          > Enterprise Technology Solutions, TD Bank Financial Group[/color]

          One can add a scalar quantity to a pointer to a new location.
          For example, "Move 3 houses north of 12 Oak Lane", makes sense.

          If two pointers can be subracted for a distance then a distance
          should be added from one pointer to generate a new pointer value.

          --
          Thomas Matthews

          C++ newsgroup welcome message:

          C++ Faq: http://www.parashift.com/c++-faq-lite
          C Faq: http://www.eskimo.com/~scs/c-faq/top.html
          alt.comp.lang.l earn.c-c++ faq:

          Other sites:
          http://www.josuttis.com -- C++ STL Library book

          Comment

          • CBFalconer

            #6
            Re: Valid operations on pointers in C

            aruna wrote:[color=blue]
            >
            > Why division/mulitiplication/addition of pointers are not
            > allowed in C?[/color]

            Using right forefinger, point to nose. Using left forefinger,
            point to right ear. Now define the product, sum, ratios of those
            pointers.

            --
            Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
            Available for consulting/temporary embedded and systems.
            <http://cbfalconer.home .att.net> USE worldnet address!


            Comment

            • Joona I Palaste

              #7
              Re: Valid operations on pointers in C

              Thomas Matthews <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > scribbled the following:[color=blue]
              > Lew Pitcher wrote:[color=green]
              >> aruna wrote:
              >> | Why division/mulitiplication/addition of pointers are not
              >> | allowed in C?
              >>
              >> A pointer can be considered to be the 'location' of a data item.
              >>
              >> Subtraction of one pointer from another gives you the 'distance' between
              >> two data items.
              >>
              >> What would addition of two pointers give you? (Hint: What does the
              >> addition of '12 Oak Lane' to '757 Main Street West' give you?)
              >>
              >> What would multiplication of two pointers give you? (Hint: What does the
              >> multiplication of '12 Oak Lane' with '757 Main Street West' give you?)
              >>
              >> What would division of two pointers give you? (Hint: What does the
              >> division of '12 Oak Lane' by '757 Main Street West' give you?)
              >>
              >> While pointer subtraction is meaningful, the other operations are not.[/color][/color]
              [color=blue]
              > One can add a scalar quantity to a pointer to a new location.
              > For example, "Move 3 houses north of 12 Oak Lane", makes sense.[/color]
              [color=blue]
              > If two pointers can be subracted for a distance then a distance
              > should be added from one pointer to generate a new pointer value.[/color]

              Where did anyone mention a scalar quantity?

              --
              /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
              \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
              "You could take his life and..."
              - Mirja Tolsa

              Comment

              • Dan Pop

                #8
                Re: Valid operations on pointers in C

                In <a2efcaed.04042 30628.18b0d5e1@ posting.google. com> arunal2001@yaho o.co.in (aruna) writes:
                [color=blue]
                >Why division/mulitiplication/addition of pointers are not
                >allowed in C?[/color]

                Try figuring out some *useful* semantics for these operations and you may
                understand why.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Tim Hagan

                  #9
                  Re: Valid operations on pointers in C

                  "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                  news:40893C86.7 E1AB484@yahoo.c om...[color=blue]
                  > aruna wrote:[color=green]
                  > >
                  > > Why division/mulitiplication/addition of pointers are not
                  > > allowed in C?[/color]
                  >
                  > Using right forefinger, point to nose. Using left forefinger,
                  > point to right ear. Now define the product, sum, ratios of those
                  > pointers.[/color]

                  This analogy is lost on me. If I subtract one of those pointers from the
                  other, then what body part would I be pointing to now?

                  --
                  Tim Hagan


                  Comment

                  • Joona I Palaste

                    #10
                    Re: Valid operations on pointers in C

                    Tim Hagan <no.spam@boeing .com.invalid> scribbled the following:[color=blue]
                    > "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                    > news:40893C86.7 E1AB484@yahoo.c om...[color=green]
                    >> aruna wrote:[color=darkred]
                    >> > Why division/mulitiplication/addition of pointers are not
                    >> > allowed in C?[/color]
                    >>
                    >> Using right forefinger, point to nose. Using left forefinger,
                    >> point to right ear. Now define the product, sum, ratios of those
                    >> pointers.[/color][/color]
                    [color=blue]
                    > This analogy is lost on me. If I subtract one of those pointers from the
                    > other, then what body part would I be pointing to now?[/color]

                    None. But you have the distance between your nose and your right ear. C
                    works like that too. Now define the product, sum and ratio of those
                    pointers. Either in real-world or C terms.

                    --
                    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                    "B-but Angus! You're a dragon!"
                    - Mickey Mouse

                    Comment

                    • Kenneth Brody

                      #11
                      Re: Valid operations on pointers in C

                      Joona I Palaste wrote:
                      [...][color=blue][color=green]
                      > > This analogy is lost on me. If I subtract one of those pointers from the
                      > > other, then what body part would I be pointing to now?[/color]
                      >
                      > None. But you have the distance between your nose and your right ear. C
                      > works like that too. Now define the product, sum and ratio of those
                      > pointers. Either in real-world or C terms.[/color]

                      The closest thing I can come up with (in real-world, but not C) is
                      (ptr1+ptr2)/2 for the average. (ie: what is midway between your nose
                      and right ear.) Of course, this is really the average of two _points_
                      and not two _pointers_.

                      And, in C, this can be done (assuming that ptr1 and ptr2 are such that
                      their difference is defined) with "ptr1 + ( (ptr2-ptr1) / 2 )", which is
                      more meaningful in a sense. (ie: start here and go halfway to there.)

                      --
                      +-------------------------+--------------------+-----------------------------+
                      | Kenneth J. Brody | www.hvcomputer.com | |
                      | kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
                      +-------------------------+--------------------+-----------------------------+

                      Comment

                      • Rakesh Kumar

                        #12
                        Re: Valid operations on pointers in C

                        Addition / subtraction is definitely allowed, the reason being pointers
                        refer to address and you can back and forth in the memory space. (Say,
                        as in an array as the memory is allocated continuously ).

                        Multiplication / Division just does not make sense, as there is no
                        necessity for them logically in a given program.

                        HTH

                        aruna wrote:[color=blue]
                        > Why division/mulitiplication/addition of pointers are not
                        > allowed in C?[/color]

                        Comment

                        • Christian Bau

                          #13
                          Re: Valid operations on pointers in C

                          In article <a2efcaed.04042 30628.18b0d5e1@ posting.google. com>,
                          arunal2001@yaho o.co.in (aruna) wrote:
                          [color=blue]
                          > Why division/mulitiplication/addition of pointers are not
                          > allowed in C?[/color]

                          Tell us what the result would be.

                          Comment

                          • Joe Wright

                            #14
                            Re: Valid operations on pointers in C

                            aruna wrote:
                            [color=blue]
                            > Why division/mulitiplication/addition of pointers are not
                            > allowed in C?[/color]

                            Pointer values are addresses. As such they are similar to postal zip
                            codes or telephone numbers. Of what possible value would be the
                            division, multiplication or addition of two or more zip codes?

                            --
                            Joe Wright mailto:joewwrig ht@comcast.net
                            "Everything should be made as simple as possible, but not simpler."
                            --- Albert Einstein ---

                            Comment

                            • Joona I Palaste

                              #15
                              Re: Valid operations on pointers in C

                              Rakesh Kumar <dreamzlion_nos pamplz@yahoo.co m> scribbled the following:[color=blue]
                              > Addition / subtraction is definitely allowed, the reason being pointers
                              > refer to address and you can back and forth in the memory space. (Say,
                              > as in an array as the memory is allocated continuously ).[/color]

                              Addition of two pointers is not allowed, addition of a pointer and a
                              scalar is.

                              --
                              /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                              \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                              "I am not very happy acting pleased whenever prominent scientists overmagnify
                              intellectual enlightenment."
                              - Anon

                              Comment

                              Working...