Why multiplication not allowed?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vijay Kumar R Zanvar

    Why multiplication not allowed?

    Hi,

    Why multiplication of pointers is not allowed?
    Till now I only know this, but not the reason why!

    PS: As a rule, I searched the FAQ, but could not
    find an answer.

    --
    Vijay Kumar R Zanvar
    My Home Page - http://www.geocities.com/vijoeyz/


  • Ben Pfaff

    #2
    Re: Why multiplication not allowed?

    "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
    [color=blue]
    > Why multiplication of pointers is not allowed?
    > Till now I only know this, but not the reason why![/color]

    What would multiplication of pointers mean?

    Comment

    • Vijay Kumar R Zanvar

      #3
      Re: Why multiplication not allowed?


      "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
      news:87ekudv9da .fsf@pfaff.stan ford.edu...[color=blue]
      > "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
      >[color=green]
      > > Why multiplication of pointers is not allowed?
      > > Till now I only know this, but not the reason why![/color]
      >
      > What would multiplication of pointers mean?[/color]

      #include <stdio.h>
      #include <stdlib.h>

      int
      main ( void )
      {
      int i = 10;
      int *k = &i, *j = &i;

      k = k * j;
      return EXIT_SUCCESS;
      }

      This gives an error:

      ptr_mul.c: In function `main':
      ptr_mul.c:10: error: invalid operands to binary *


      Comment

      • Ben Pfaff

        #4
        Re: Why multiplication not allowed?

        "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
        [color=blue]
        > "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
        > news:87ekudv9da .fsf@pfaff.stan ford.edu...[color=green]
        > > "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
        > >[color=darkred]
        > > > Why multiplication of pointers is not allowed?
        > > > Till now I only know this, but not the reason why![/color]
        > >
        > > What would multiplication of pointers mean?[/color]
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        >
        > int
        > main ( void )
        > {
        > int i = 10;
        > int *k = &i, *j = &i;
        >
        > k = k * j;[/color]

        You mean
        *k = *k * *j;
        In other words, multiply `int's, not pointers to `int's.
        [color=blue]
        > return EXIT_SUCCESS;
        > }[/color]

        The difference between pointers and their referents is pretty
        fundamental. Have you considered reading a textbook or taking a
        class?
        --
        "I don't have C&V for that handy, but I've got Dan Pop."
        --E. Gibbons

        Comment

        • Julian Maisano

          #5
          Re: Why multiplication not allowed?


          "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
          news:877k05v8c2 .fsf@pfaff.stan ford.edu...[color=blue][color=green]
          > > int i = 10;
          > > int *k = &i, *j = &i;
          > >
          > > k = k * j;[/color]
          >
          > You mean
          > *k = *k * *j;
          > In other words, multiply `int's, not pointers to `int's.
          >[/color]

          Is the following code wrong? I mean, does it do the same?
          int* k =&i
          int* j=&i
          k* = k* * j*

          (It seems to me more readable than the above. I come from Pascal background)






          Comment

          • Kevin Goodsell

            #6
            Re: Why multiplication not allowed?

            Vijay Kumar R Zanvar wrote:[color=blue]
            > "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
            > news:87ekudv9da .fsf@pfaff.stan ford.edu...
            >[color=green]
            >>"Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
            >>
            >>[color=darkred]
            >>> Why multiplication of pointers is not allowed?
            >>>Till now I only know this, but not the reason why![/color]
            >>
            >>What would multiplication of pointers mean?[/color]
            >[/color]

            The question Ben is asking is, what would you propose pointer
            multiplication do? How would you define the operation? I can't think of
            any way in which multiplying a pointer by any other value would be
            useful or even meaningful.

            Your question is somewhat like asking "why can't I assign to a
            function?" or "why can't I take the square root of a struct?" There's
            just no logical reason why you *should* be able to.

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • Richard Bos

              #7
              Re: Why multiplication not allowed?

              "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> wrote:
              [color=blue]
              > Why multiplication of pointers is not allowed?[/color]

              Jack lives at Westmoreland Street 12, London SW6 4E8. Jill lives at
              Rossinistraat 27b, 1287 CZ Amsterdam. Please multiply these addresses
              and let us know the result.

              Richard

              Comment

              • Richard Heathfield

                #8
                Re: Why multiplication not allowed?

                Julian Maisano wrote:
                [color=blue]
                >
                > "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
                > news:877k05v8c2 .fsf@pfaff.stan ford.edu...[color=green][color=darkred]
                >> > int i = 10;
                >> > int *k = &i, *j = &i;
                >> >
                >> > k = k * j;[/color]
                >>
                >> You mean
                >> *k = *k * *j;
                >> In other words, multiply `int's, not pointers to `int's.
                >>[/color]
                >
                > Is the following code wrong? I mean, does it do the same?
                > int* k =&i
                > int* j=&i
                > k* = k* * j*[/color]

                Yes, it's wrong; no, it doesn't do the same thing; and no, it's not legal
                syntax. The last line parses as:

                k *= k * *j *

                That is, multiply k (a pointer!) by the contents of j, multiply /that/ by
                heaven knows what, and then use the result to multiply by k again,
                assigning the result to k.
                [color=blue]
                > (It seems to me more readable than the above. I come from Pascal
                > background)[/color]

                In C, the dereferencing operator comes before the pointer. C programmers are
                accustomed to this syntax, and find it perfectly readable.


                --
                Richard Heathfield : binary@eton.pow ernet.co.uk
                "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
                C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                K&R answers, C books, etc: http://users.powernet.co.uk/eton

                Comment

                • Jeremy Yallop

                  #9
                  Re: Why multiplication not allowed?

                  Richard Heathfield wrote:[color=blue]
                  > Julian Maisano wrote:[color=green]
                  >> Is the following code wrong? I mean, does it do the same?
                  >> int* k =&i
                  >> int* j=&i
                  >> k* = k* * j*[/color]
                  >
                  > Yes, it's wrong; no, it doesn't do the same thing; and no, it's not legal
                  > syntax. The last line parses as:
                  >
                  > k *= k * *j *[/color]

                  It doesn't parse that way. The whitespace between '*' and '=' is
                  significant, so

                  i* = j;

                  is also a syntax error.

                  Jeremy.

                  Comment

                  • Richard Heathfield

                    #10
                    Re: Why multiplication not allowed?

                    Jeremy Yallop wrote:
                    [color=blue]
                    > Richard Heathfield wrote:[color=green]
                    >> Julian Maisano wrote:[color=darkred]
                    >>> Is the following code wrong? I mean, does it do the same?
                    >>> int* k =&i
                    >>> int* j=&i
                    >>> k* = k* * j*[/color]
                    >>
                    >> Yes, it's wrong; no, it doesn't do the same thing; and no, it's not legal
                    >> syntax. The last line parses as:
                    >>
                    >> k *= k * *j *[/color]
                    >
                    > It doesn't parse that way. The whitespace between '*' and '=' is
                    > significant,[/color]

                    Indeed it is. Oops, sorry, darn and heck. (More or less in that order.)
                    [color=blue]
                    > so
                    >
                    > i* = j;
                    >
                    > is also a syntax error.[/color]

                    Right.

                    --
                    Richard Heathfield : binary@eton.pow ernet.co.uk
                    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
                    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                    K&R answers, C books, etc: http://users.powernet.co.uk/eton

                    Comment

                    • Eric Sosman

                      #11
                      Re: Why multiplication not allowed?

                      Vijay Kumar R Zanvar wrote:[color=blue]
                      >
                      > Hi,
                      >
                      > Why multiplication of pointers is not allowed?
                      > Till now I only know this, but not the reason why!
                      >
                      > PS: As a rule, I searched the FAQ, but could not
                      > find an answer.[/color]

                      The result would be meaningless. An easy (but
                      informal) way to understand this is to use an analogy:
                      think of pointer values as street addresses. Then
                      the following operations make sense:

                      - Find the house at "123 Main Street."

                      - To find the neigboring house, compute "123
                      Main Street plus one." (I'm ignoring such
                      real-world intrusions as odd-even numbering
                      schemes, discontinuities between city blocks,
                      and so on: we're just exploring an imperfect
                      analogy, after all.)

                      - To find the distance between two Main Street
                      addresses, compute "123 Main Street minus 189
                      Main Street," yielding "minus 66 lots."

                      However, some other arithmetical operations make
                      no sense at all:

                      - Computing "123 Main Street plus 207 Main Street"
                      produces no useful answer.

                      - Computing "123 Main Street minus 123 Elm Street"
                      produces no useful answer.

                      - Similarly, computing "123 Main Street times
                      89 El Camino Real" makes no sense. Perhaps the
                      result might be considered a kind of "area,"
                      but there seems to be no useful analogous
                      concept to "area" in the addressing of memory-
                      resident objects.

                      - Finally, computing "123 Main Street times three"
                      might possibly make sense, arriving at "369 Main
                      Street." But such a definition carries a built-
                      in assumption that Main Street is zero-based,
                      and in a linear computer memory no more than one
                      object can begin at "address zero." If you want
                      to find the house three times as far from the
                      start of the street, you really want to compute
                      "Main Street Origin plus three times (123 Main
                      Street minus Main Street Origin)."

                      --
                      Eric.Sosman@sun .com

                      Comment

                      • Sean Kenwrick

                        #12
                        Re: Why multiplication not allowed?


                        "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
                        news:877k05v8c2 .fsf@pfaff.stan ford.edu...[color=blue]
                        > "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
                        >[color=green]
                        > > "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
                        > > news:87ekudv9da .fsf@pfaff.stan ford.edu...[color=darkred]
                        > > > "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
                        > > >
                        > > > > Why multiplication of pointers is not allowed?
                        > > > > Till now I only know this, but not the reason why!
                        > > >
                        > > > What would multiplication of pointers mean?[/color]
                        > >
                        > > #include <stdio.h>
                        > > #include <stdlib.h>
                        > >
                        > > int
                        > > main ( void )
                        > > {
                        > > int i = 10;
                        > > int *k = &i, *j = &i;
                        > >
                        > > k = k * j;[/color]
                        >
                        > You mean
                        > *k = *k * *j;
                        > In other words, multiply `int's, not pointers to `int's.
                        >[/color]

                        Your solution above is correct but can you figure out why the following code
                        is invalid (which it is):


                        *k = *k/*j;

                        assuming k and j are pointers to ints that have been correctly
                        initialized....

                        Sean


                        Comment

                        • Jarno A Wuolijoki

                          #13
                          Re: Why multiplication not allowed?

                          On Tue, 6 Jan 2004, Sean Kenwrick wrote:
                          [color=blue]
                          > Your solution above is correct but can you figure out why the following code
                          > is invalid (which it is):
                          > *k = *k/*j;[/color]

                          *k = k*//*j;

                          Comment

                          • Jarno A Wuolijoki

                            #14
                            Re: Why multiplication not allowed?

                            On Tue, 6 Jan 2004, Jarno A Wuolijoki wrote:
                            [color=blue][color=green]
                            > > Your solution above is correct but can you figure out why the following code
                            > > is invalid (which it is):
                            > > *k = *k/*j;[/color]
                            >
                            > *k = k*//*j;[/color]

                            Should have engaged my brain. It starts a new one, duh.

                            Comment

                            • Ben Pfaff

                              #15
                              Re: Why multiplication not allowed?

                              "Sean Kenwrick" <skenwrick@hotm ail.com> writes:
                              [color=blue]
                              > Your solution above is correct but can you figure out why the following code
                              > is invalid (which it is):
                              >
                              >
                              > *k = *k/*j;[/color]

                              There's no way to tell whether it's invalid unless we can see
                              what follows it.
                              --
                              "Welcome to the wonderful world of undefined behavior, where the demons
                              are nasal and the DeathStation users are nervous." --Daniel Fox

                              Comment

                              Working...