Pointer question

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

    #1

    Pointer question

    Hi,
    to swap the integer variables a,b,
    in Kerninghan-Ritchie I find the function example

    Swap(&a,&b);

    unsing a function

    void swap (int *px, int *py)
    {
    int temp;
    temp:= *px;
    *px = *py;
    *py = temp
    }

    Now here I have a problem understanding that.
    The function gets two !Pointers! an arguments.

    So as far as I see *px=&a will happen, but *px is an integer
    variable and &a is a pointer. But I think px=&a would be
    correct.

    ????
    Thanks in advance
    Erik
  • Christian Bau

    #2
    Re: Pointer question

    In article <c16042a8.04072 72357.1d21584@p osting.google.c om>,
    temporary1234@q uantentunnel.de (Erik) wrote:
    [color=blue]
    > Hi,
    > to swap the integer variables a,b,
    > in Kerninghan-Ritchie I find the function example
    >
    > Swap(&a,&b);
    >
    > unsing a function
    >
    > void swap (int *px, int *py)
    > {
    > int temp;
    > temp:= *px;
    > *px = *py;
    > *py = temp
    > }[/color]

    You should really try hard to get your spelling correct. The code that
    you wrote has no chance of ever compiling, and even if it did, what does
    the function "swap" have to do with the function "Swap" that you are
    trying to call?

    I am sure that Mr. Ritchie has never written a book together with anyone
    by the name of Kerninghan. I know the word "sing" but I have no idea
    what you mean by "unsing". If you think I am too pedantic, then
    programming is not for you.

    Comment

    • Ramiro Rodriguez

      #3
      Re: Pointer question

      > So as far as I see *px=&a will happen, but *px is an integer[color=blue]
      > variable and &a is a pointer. But I think px=&a would be
      > correct.
      >
      > ????
      > Thanks in advance
      > Erik[/color]

      In the function declaration px is declared as a variable of type int*. *px
      is not the variable that gets set when the function is called it is px
      which gets set to &a.

      -RR

      Comment

      • Rowland

        #4
        Re: Pointer question


        "Christian Bau" <christian.bau@ cbau.freeserve. co.uk> wrote in message
        news:christian. bau-5D99B5.09123528 072004@slb-newsm1.svr.pol. co.uk...[color=blue]
        > In article <c16042a8.04072 72357.1d21584@p osting.google.c om>,
        > temporary1234@q uantentunnel.de (Erik) wrote:
        >[color=green]
        > > Hi,
        > > to swap the integer variables a,b,
        > > in Kerninghan-Ritchie I find the function example
        > >
        > > Swap(&a,&b);
        > >
        > > unsing a function
        > >
        > > void swap (int *px, int *py)
        > > {
        > > int temp;
        > > temp:= *px;
        > > *px = *py;
        > > *py = temp
        > > }[/color]
        >
        > You should really try hard to get your spelling correct. The code that
        > you wrote has no chance of ever compiling, and even if it did, what does
        > the function "swap" have to do with the function "Swap" that you are
        > trying to call?[/color]

        That's a fair point as code should be correct when it's posted.
        [color=blue]
        > I am sure that Mr. Ritchie has never written a book together with anyone
        > by the name of Kerninghan. I know the word "sing" but I have no idea
        > what you mean by "unsing". If you think I am too pedantic, then
        > programming is not for you.[/color]

        I don't think this is fair though, as the chap appears to be German, and
        probably
        has a certain amount of difficulty with English, it not being his native
        language.
        Criticism of one typographical error and one misspelling, which do not
        affect comprehension of
        his message, is unwarranted. If it was unreadable garbage I would agree with
        you,
        but it's not.

        Rowland.


        Comment

        • Christian Bau

          #5
          Re: Pointer question

          In article <ce7pm1$ea9$1@s parta.btinterne t.com>,
          "Rowland" <banksr0@hotmai l.com> wrote:
          [color=blue]
          > "Christian Bau" <christian.bau@ cbau.freeserve. co.uk> wrote in message
          > news:christian. bau-5D99B5.09123528 072004@slb-newsm1.svr.pol. co.uk...[/color]
          [color=blue][color=green]
          > > I am sure that Mr. Ritchie has never written a book together with anyone
          > > by the name of Kerninghan. I know the word "sing" but I have no idea
          > > what you mean by "unsing". If you think I am too pedantic, then
          > > programming is not for you.[/color]
          >
          > I don't think this is fair though, as the chap appears to be German, and
          > probably
          > has a certain amount of difficulty with English, it not being his native
          > language.
          > Criticism of one typographical error and one misspelling, which do not
          > affect comprehension of
          > his message, is unwarranted. If it was unreadable garbage I would agree with
          > you,
          > but it's not.[/color]

          Spelling "Kernighan" correctly has nothing to do with your native
          language. And I am one hundred percent sure that there is not one German
          who knows the word "using" and would believe that it is spelt as
          "unsing".

          Comment

          • Rowland

            #6
            Re: Pointer question


            "Christian Bau" <christian.bau@ cbau.freeserve. co.uk> wrote in message
            news:christian. bau-E5DD3A.10202428 072004@slb-newsm1.svr.pol. co.uk...[color=blue]
            > In article <ce7pm1$ea9$1@s parta.btinterne t.com>,
            > "Rowland" <banksr0@hotmai l.com> wrote:
            >[color=green]
            > > "Christian Bau" <christian.bau@ cbau.freeserve. co.uk> wrote in message
            > > news:christian. bau-5D99B5.09123528 072004@slb-newsm1.svr.pol. co.uk...[/color]
            >[color=green][color=darkred]
            > > > I am sure that Mr. Ritchie has never written a book together with[/color][/color][/color]
            anyone[color=blue][color=green][color=darkred]
            > > > by the name of Kerninghan. I know the word "sing" but I have no idea
            > > > what you mean by "unsing". If you think I am too pedantic, then
            > > > programming is not for you.[/color]
            > >
            > > I don't think this is fair though, as the chap appears to be German, and
            > > probably
            > > has a certain amount of difficulty with English, it not being his native
            > > language.
            > > Criticism of one typographical error and one misspelling, which do not
            > > affect comprehension of
            > > his message, is unwarranted. If it was unreadable garbage I would agree[/color][/color]
            with[color=blue][color=green]
            > > you,
            > > but it's not.[/color]
            >
            > Spelling "Kernighan" correctly has nothing to do with your native
            > language. And I am one hundred percent sure that there is not one German
            > who knows the word "using" and would believe that it is spelt as
            > "unsing".[/color]

            I do agree that there is not one German. Perhaps two, or even three ("If you
            think I am too pedantic, then programming is not for you." (sorry, couldn't
            resist!)).

            You're right in that I should have said two typos. I still feel it was a
            slight over-reaction to two minor mistakes, and unhelpful to the discussion
            though. I don't mean to flame, and I agree that it's a very subjective line
            as to when somebodies lack of grammar/punctuation/spelling/etc. becomes a
            problem. All IMHO, naturally ;-)

            Warm regards,

            Rowland.


            Comment

            • SM Ryan

              #7
              Re: Pointer question

              temporary1234@q uantentunnel.de (Erik) wrote:
              # Hi,
              # to swap the integer variables a,b,
              # in Kerninghan-Ritchie I find the function example
              #
              # Swap(&a,&b);
              #
              # unsing a function
              #
              # void swap (int *px, int *py)
              # {
              # int temp;
              # temp:= *px;
              # *px = *py;
              # *py = temp
              # }
              #
              # Now here I have a problem understanding that.
              # The function gets two !Pointers! an arguments.
              #
              # So as far as I see *px=&a will happen, but *px is an integer
              # variable and &a is a pointer. But I think px=&a would be
              # correct.

              If you're referring to the explanation of function call by inlining
              the function text, what you've really got is

              Swap(&a,&b);
              ---------------------------
              {
              int *px = &a;
              int *py = &b;
              {
              int temp;
              temp = *px;
              *px = *py;
              *py = temp
              }
              }

              So that implied parameter assignment is equivalent to the initialiser
              in a declaration, not to the assignment expression in a statement.
              In a declaration, *px does not mean dereference px, but rather it
              declares that px can be dereferenced in subsequent statements.

              --
              SM Ryan http://www.rawbw.com/~wyrmwif/
              I think that's kinda of personal; I don't think I should answer that.

              Comment

              • Martin Ambuhl

                #8
                Re: Pointer question

                Erik wrote:
                [color=blue]
                >
                > void swap (int *px, int *py)
                > {
                > int temp;
                > temp:= *px;
                > *px = *py;
                > *py = temp
                > }
                >
                > Now here I have a problem understanding that.
                > The function gets two !Pointers! an arguments.
                >
                > So as far as I see *px=&a will happen, but *px is an integer
                > variable and &a is a pointer. But I think px=&a would be
                > correct.[/color]

                It is an error to think that the assignment "*px=&a will happen". 'px'
                is a pointer to int, and it is px (not *px) set to &a. What you think
                "would be correct" is in fact what happens.

                Comment

                • Martin Ambuhl

                  #9
                  Re: Pointer question

                  Christian Bau wrote:
                  [color=blue]
                  > In article <c16042a8.04072 72357.1d21584@p osting.google.c om>,
                  > temporary1234@q uantentunnel.de (Erik) wrote:
                  >
                  >[color=green]
                  >>Hi,
                  >>to swap the integer variables a,b,
                  >>in Kerninghan-Ritchie I find the function example
                  >>
                  >>Swap(&a,&b) ;
                  >>
                  >>unsing a function
                  >>
                  >>void swap (int *px, int *py)
                  >>{
                  >>int temp;
                  >>temp:= *px;
                  >>*px = *py;
                  >>*py = temp
                  >>}[/color]
                  >
                  >
                  > You should really try hard to get your spelling correct.[/color]

                  While case is important, so the mistake of 'Swap' for 'swap' matters,
                  the typo of 'unsing' and misspelling 'Kerningham' hardly matter.
                  However, the misspelling of ':=' for the assignment operator is not only
                  important, but suggests a background in a language from Switzerland that
                  does _not_ distinguish between 'Swap' and 'swap'.
                  [color=blue]
                  > The code that
                  > you wrote has no chance of ever compiling, and even if it did, what does
                  > the function "swap" have to do with the function "Swap" that you are
                  > trying to call?
                  >
                  > I am sure that Mr. Ritchie has never written a book together with anyone
                  > by the name of Kerninghan. I know the word "sing" but I have no idea
                  > what you mean by "unsing". If you think I am too pedantic, then
                  > programming is not for you.[/color]

                  Comment

                  Working...