Passing by Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • irwishlaw@gmail.com

    #1

    Passing by Value

    In Chapter 2 of the book 'C Unleashed' the author states that this
    code

    #include <stdio.h
    void increment(char *p)
    {
    ++p
    }

    int main(void)
    {
    char *s = "Hello world";
    increment(s);
    printf("%s\n", s);
    return 0;
    }

    "doesn't work (and in fact results in undefined behavior):"

    Modified code that does work, that is, code that prints "ello world"
    is not shown.

    What is is proper way to modify the code so that "ello world" is
    printed?

    Robert Wishlaw

  • Peter Nilsson

    #2
    Re: Passing by Value

    irwish...@gmail .com wrote:
    In Chapter 2 of the book 'C Unleashed' the author states that
    this code
    >
    #include <stdio.h
    Did you mean #include <stdio.h ?
    void increment(char *p)
    {
    ++p
    Did you mean ++p; ?
    }
    >
    int main(void)
    {
    char *s = "Hello world";
    increment(s);
    printf("%s\n", s);
    return 0;
    }
    >
    "doesn't work (and in fact results in undefined behavior):"
    Undefined behaviour? Was it originally ++*p; ?
    Modified code that does work, that is, code that prints "ello
    world" is not shown.
    >
    What is is proper way to modify the code so that "ello world" is
    printed?
    'Proper' is relative, but the following suffices...

    void increment(char **p) /* pointer to a pointer */
    {
    ++(*p); /* parentheses are actually redundant */
    }

    increment(&s);

    --
    Peter

    Comment

    • Ben Pfaff

      #3
      Re: Passing by Value

      irwishlaw@gmail .com writes:
      In Chapter 2 of the book 'C Unleashed' the author states that this
      code
      [...syntacticall y incorrect code and incorrect conclusion...]
      I never would have thought that Richard Heathfield would make so
      many mistakes about basic stuff. Live and learn, I guess.
      --
      Ben Pfaff

      Comment

      • irwishlaw@gmail.com

        #4
        Re: Passing by Value

        On Apr 4, 7:30 pm, "Peter Nilsson" <a...@acay.com. auwrote:
        irwish...@gmail .com wrote:
        In Chapter 2 of the book 'C Unleashed' the author states that
        this code
        >
        #include <stdio.h
        >
        Did you mean #include <stdio.h ?
        >
        Yes.
        void increment(char *p)
        {
        ++p
        >
        Did you mean ++p; ?
        >
        Yes.
        }
        >
        int main(void)
        {
        char *s = "Hello world";
        increment(s);
        printf("%s\n", s);
        return 0;
        }
        >
        "doesn't work (and in fact results in undefined behavior):"
        >
        Undefined behaviour? Was it originally ++*p; ?
        >
        Modified code that does work, that is, code that prints "ello
        world" is not shown.
        >
        What is is proper way to modify the code so that "ello world" is
        printed?
        >
        'Proper' is relative, but the following suffices...
        >
        void increment(char **p) /* pointer to a pointer */
        {
        ++(*p); /* parentheses are actually redundant */
        }
        >
        increment(&s);
        >
        Thank you.
        --
        Peter

        Comment

        • irwishlaw@gmail.com

          #5
          Re: Passing by Value

          On Apr 4, 7:30 pm, "Peter Nilsson" <a...@acay.com. auwrote:
          irwish...@gmail .com wrote:
          In Chapter 2 of the book 'C Unleashed' the author states that
          this code
          >
          #include <stdio.h
          >
          Did you mean #include <stdio.h ?
          >
          void increment(char *p)
          {
          ++p
          >
          Did you mean ++p; ?
          >
          }
          >
          int main(void)
          {
          char *s = "Hello world";
          increment(s);
          printf("%s\n", s);
          return 0;
          }
          >
          "doesn't work (and in fact results in undefined behavior):"
          >
          Undefined behaviour? Was it originally ++*p; ?
          >
          No, the code in the book is ++p;.

          Modified code that does work, that is, code that prints "ello
          world" is not shown.
          >
          What is is proper way to modify the code so that "ello world" is
          printed?
          >
          'Proper' is relative, but the following suffices...
          >
          void increment(char **p) /* pointer to a pointer */
          {
          ++(*p); /* parentheses are actually redundant */
          }
          >
          increment(&s);
          >
          --
          Peter

          Comment

          • Richard Heathfield

            #6
            Re: Passing by Value

            irwishlaw@gmail .com said:
            In Chapter 2 of the book 'C Unleashed' the author states that this
            code
            >
            #include <stdio.h
            void increment(char *p)
            {
            ++p
            }
            >
            int main(void)
            {
            char *s = "Hello world";
            increment(s);
            printf("%s\n", s);
            return 0;
            }
            >
            "doesn't work (and in fact results in undefined behavior):"
            I state no such thing. What I actually say is:

            (begin quote)

            What is actually happening here is that the addresses [...] are being
            passed - by value. We can see this most clearly with a simple
            demonstration program:

            #include <stdio.h>
            void increment(char *p)
            {
            ++p;
            }

            int main(void)
            {
            char *s = "Hello world";
            increment(s);
            printf("%s\n", s);
            return 0;
            }

            If C passed pointers by reference, this program would print

            ello world

            But it doesn't. It prints

            Hello world

            This indicates that the modification of p by the increment() function
            has no effect on the original pointer s. This is exactly the behavior
            we would expect of pass-by-value. For precisely the same reason, this
            code doesn't work (and in fact results in undefined behavior):

            #include <stdio.h>

            int openfile(char *s, FILE *fp)
            {
            fp = fopen(s, "r");
            return fp ? 1 : 0;
            }

            int main(void)
            {
            FILE *fp;
            char buffer[1024];
            if(openfile("re adme", fp))
            {
            while(fgets(buf fer, sizeof buffer, fp))
            printf("%s", buffer);
            fclose(fp);
            printf("\n");
            }
            return 0;
            }

            (end quote)

            The original text, btw, spelled "behavior" as "behaviour" , but it got
            mangled during production (apparently on purpose).
            >
            Modified code that does work, that is, code that prints "ello world"
            is not shown.
            >
            What is is proper way to modify the code so that "ello world" is
            printed?
            This has already been answered elsethread.

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at the above domain, - www.

            Comment

            • Richard Heathfield

              #7
              Re: Passing by Value

              Ben Pfaff said:
              irwishlaw@gmail .com writes:
              >
              >In Chapter 2 of the book 'C Unleashed' the author states that this
              >code
              >[...syntacticall y incorrect code and incorrect conclusion...]
              >
              I never would have thought that Richard Heathfield would make so
              many mistakes about basic stuff. Live and learn, I guess.
              To get the best out of this book, I strongly recommend that you read it.
              :-)

              --
              Richard Heathfield
              "Usenet is a strange place" - dmr 29/7/1999

              email: rjh at the above domain, - www.

              Comment

              • CBFalconer

                #8
                Re: Passing by Value

                Ben Pfaff wrote:
                irwishlaw@gmail .com writes:
                >
                >In Chapter 2 of the book 'C Unleashed' the author states that this
                >code [...syntacticall y incorrect code and incorrect conclusion...]
                >
                I never would have thought that Richard Heathfield would make so
                many mistakes about basic stuff. Live and learn, I guess.
                Nicely snide, and slyly inserted. Now twist.

                --
                <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                <http://www.securityfoc us.com/columnists/423>
                <http://www.aaxnet.com/editor/edit043.html>

                "A man who is right every time is not likely to do very much."
                -- Francis Crick, co-discover of DNA
                "There is nothing more amazing than stupidity in action."
                -- Thomas Matthews



                --
                Posted via a free Usenet account from http://www.teranews.com

                Comment

                • Martin Ambuhl

                  #9
                  Re: Passing by Value

                  irwishlaw@gmail .com wrote:
                  In Chapter 2 of the book 'C Unleashed' the author states that this
                  code
                  There are two many typographical errors for this to really be the code.
                  #include <stdio.h
                  void increment(char *p)
                  {
                  ++p
                  }
                  int main(void)
                  {
                  char *s = "Hello world";
                  increment(s);
                  printf("%s\n", s);
                  return 0;
                  }
                  "doesn't work (and in fact results in undefined behavior):"
                  Modified code that does work, that is, code that prints "ello world"
                  is not shown.
                  What is is proper way to modify the code so that "ello world" is
                  printed?

                  #include <stdio.h /* mha: added '>' */

                  void increment(char **p /* mha: added '*' */ )
                  {
                  ++*p; /* mha: added ';', added '*' */
                  }

                  int main(void)
                  {
                  char *s = "Hello world";
                  increment(&s); /* mha: added '&' */
                  printf("%s\n", s);
                  return 0;
                  }

                  Comment

                  • Richard Heathfield

                    #10
                    Re: Passing by Value

                    Martin Ambuhl said:
                    irwishlaw@gmail .com wrote:
                    >In Chapter 2 of the book 'C Unleashed' the author states that this
                    >code
                    >
                    There are two many typographical errors for this to really be the
                    code.
                    Presumably on the grounds that two is two many. :-) Incidentally, the
                    OP's other claim (that I had said the behaviour of the code is
                    undefined) is also incorrect. See my parallel reply.

                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at the above domain, - www.

                    Comment

                    • irwishlaw@gmail.com

                      #11
                      Re: Passing by Value

                      On Apr 4, 10:13 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                      Martin Ambuhl said:
                      >
                      irwish...@gmail .com wrote:
                      In Chapter 2 of the book 'C Unleashed' the author states that this
                      code
                      >
                      There are two many typographical errors for this to really be the
                      code.
                      >
                      Presumably on the grounds that two is two many. :-) Incidentally, the
                      OP's other claim (that I had said the behaviour of the code is
                      undefined) is also incorrect. See my parallel reply.
                      >
                      I apologize for my mistake. Upon rereading the paragraph I realize
                      that the last "this" in the paragraph refered to the following code
                      snippet and not the previous code snippet as I assumed and implied in
                      my original post.

                      I also apologize for the typographical errors in my post which could
                      have been avoided by simple cut and paste if the your statement on
                      page 15 that "The CD contains ... the complete text of the book
                      itself" was true. In fact the only code snippet on the CD from chapter
                      2 is obscure.c.

                      My main point in my original post was to find out how the example
                      cited could be rewritten to print "ello world". Peter Nilsson
                      graciously answered that question for which I am grateful.

                      Robert Wishlaw
                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
                      email: rjh at the above domain, - www.

                      Comment

                      • Richard Heathfield

                        #12
                        Re: Passing by Value

                        irwishlaw@gmail .com said:
                        I apologize for my mistake. Upon rereading the paragraph I realize
                        that the last "this" in the paragraph refered to the following code
                        snippet and not the previous code snippet as I assumed and implied in
                        my original post.
                        And I apologise in turn for originally writing it in such a way that it
                        could be misinterpreted.

                        --
                        Richard Heathfield
                        "Usenet is a strange place" - dmr 29/7/1999

                        email: rjh at the above domain, - www.

                        Comment

                        Working...