Help with code snippet

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

    Help with code snippet

    I've got a take home final for my computer class and it has a bonus
    question concerning
    some programming that we didn't have time to cover. I've already got
    an A in the class, so
    it won't affect my grade one way or another, however my obsessive/
    compulsive tendencies
    just can't let it go. Could I get some help in explaining what this
    code snippet does;

    char foo[8]=" bcd f\n\0";
    int i=2;

    while (foo[i++]) putchar(foo[i]);

    Thanks in advance,
    Cougar

  • Robert Bauck Hamar

    #2
    Re: Help with code snippet

    cougre.j@gmail. com wrote:
    I've got a take home final for my computer class and it has a bonus
    question concerning
    some programming that we didn't have time to cover. I've already got
    an A in the class, so
    it won't affect my grade one way or another, however my obsessive/
    compulsive tendencies
    just can't let it go. Could I get some help in explaining what this
    code snippet does;
    >
    char foo[8]=" bcd f\n\0";
    int i=2;
    >
    while (foo[i++]) putchar(foo[i]);
    Why don't you try it?

    --
    rbh

    Comment

    • Christopher.E.Sanborn@gmail.com

      #3
      Re: Help with code snippet

      On Jul 9, 10:30 am, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
      wrote:
      cougr...@gmail. com wrote:
      I've got a take home final for my computer class and it has a bonus
      question concerning
      some programming that we didn't have time to cover. I've already got
      an A in the class, so
      it won't affect my grade one way or another, however my obsessive/
      compulsive tendencies
      just can't let it go. Could I get some help in explaining what this
      code snippet does;
      >
      char foo[8]=" bcd f\n\0";
      int i=2;
      >
      while (foo[i++]) putchar(foo[i]);
      >
      Why don't you try it?
      >
      --
      rbh
      Well, I don't have access to C so I can't try it. From what I've been
      able to glean from the book is that its declaring an array of 8
      integers
      and increasing it by 2 during each loop. I don't understand the 'bcd'
      part.
      Can someone give me a hand?

      Comment

      • cringecoder@gmail.com

        #4
        Re: Help with code snippet

        On Jul 9, 1:00 pm, cougr...@gmail. com wrote:
        I've got a take home final for my computer class and it has a bonus
        question concerning
        some programming that we didn't have time to cover. I've already got
        an A in the class, so
        it won't affect my grade one way or another, however my obsessive/
        compulsive tendencies
        just can't let it go. Could I get some help in explaining what this
        code snippet does;
        >
        char foo[8]=" bcd f\n\0";
        int i=2;
        >
        while (foo[i++]) putchar(foo[i]);
        >
        Thanks in advance,
        Cougar
        Declares an 8-character wide array, and puts in it ' ', 'b', 'c', 'd',
        ' ', 'f', '\n', '\0' ('\n' is newline, '\0' is the null terminating
        character)
        Then int i is declared, and later is used as an index. It starts at
        position two (the third character, c, in the array foo)

        while(foo[i++]) putchar(foo[i]);

        Is essentially the same as...

        while(foo[i])
        {
        i++;
        putchar(foo[i]);
        }

        The null terminating character ('\0') is the only character that's
        zero as an integer and will terminate the loop. So , while the
        character at the current index is not the null-terminator, and after
        incrementing the index, print the character to stdout.
        It evaluates to true for 'c', then is incremented, and prints out 'd'.
        Then it evaluates true for 'd' and prints the next character, a space.
        Then it evaluates true for the space and prints the next character,
        'f', etc... Until it finally evaluates false for '\0' (In C, zero is
        considered false and anything else is considered true).

        Did this help at all? And good job getting an A!

        -Dan

        Comment

        • Marcus Kwok

          #5
          Re: Help with code snippet

          cringecoder@gma il.com wrote:
          On Jul 9, 1:00 pm, cougr...@gmail. com wrote:
          > char foo[8]=" bcd f\n\0";
          >
          Declares an 8-character wide array, and puts in it ' ', 'b', 'c', 'd',
          ' ', 'f', '\n', '\0' ('\n' is newline, '\0' is the null terminating
          character)
          When declaring a string literal, it automatically adds the null
          terminator to the end. Therefore, when I try to include the line above
          in a program, VS 2005 tells me that the array bounds overflow, since it
          is trying to put {' ', 'b', 'c', 'd', ' ', 'f', '\n', '\0', '\0'} (which
          is 9 characters) into an array that can hold only 8 characters.

          --
          Marcus Kwok
          Replace 'invalid' with 'net' to reply

          Comment

          • Juha Nieminen

            #6
            Re: Help with code snippet

            cringecoder@gma il.com wrote:
            > char foo[8]=" bcd f\n\0";
            >
            Declares an 8-character wide array, and puts in it ' ', 'b', 'c', 'd',
            ' ', 'f', '\n', '\0' ('\n' is newline, '\0' is the null terminating
            character)
            Isn't " bcd f\n\0" actually a const char[9]? What happens if you
            initialize a char[8] with it? Is that valid?

            Comment

            • Robert Bauck Hamar

              #7
              Re: Help with code snippet

              Christopher.E.S anborn@gmail.co m wrote:
              On Jul 9, 10:30 am, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
              wrote:
              > cougr...@gmail. com wrote:
              I've got a take home final for my computer class and it has a bonus
              question concerning
              some programming that we didn't have time to cover. I've already got
              an A in the class, so
              it won't affect my grade one way or another, however my obsessive/
              compulsive tendencies
              just can't let it go. Could I get some help in explaining what this
              code snippet does;
              >>
              char foo[8]=" bcd f\n\0";
              int i=2;
              >>
              while (foo[i++]) putchar(foo[i]);
              >>
              >Why don't you try it?
              Well, I don't have access to C so I can't try it.
              This is a C++ newsgroup, so you don't need C. ;-)
              From what I've been
              able to glean from the book is that its declaring an array of 8
              integers
              Yes. The integers are of type char.

              Then this array is initialised with nine elements, which is an error. You
              are not allowed to initialise an array with more elements than it can hold.
              Thus, the answer to your question: It does not compile.
              and increasing it by 2 during each loop.
              You can't increase an array.
              I don't understand the 'bcd'
              part.
              bcd stands in between the double quotes, so those characters are part of a
              string literal: " bcd f\n\0".
              Can someone give me a hand?
              If we change your example to this:
              #include <stdio.h>

              int main() {
              char foo[/*look nothing*/] = " bcd f\n\0";
              int i=2;
              while (foo[i++]) putchar(foo[i]);
              }

              then

              char foo[/*look nothing*/] = " bcd f\n\0";

              defines foo to be a char array of nine elements:
              ' ' (space), b, c, d, ' ', f, \n (newline character), \0 (character with
              value 0), and \0 (This is an extra 0. String literals always hold an extra
              0 at the end.)

              So foo[0] == ' ', foo[1] == 'b', foo[2] == 'c' ... foo[8] == char(0).

              int i=2;

              defines i to be an int, and initialises it to 2.

              while (foo[i++]) putchar(foo[i]);

              1. Checks whether the ith element of foo is not zero.
              2. Increments i.
              3. If the test in 1 was true, it writes the (new) ith element of foo to the
              standard output stream. If the test was false, it continues.

              }

              returns the value 0 to the environment.

              --
              rbh

              Comment

              • ajones@i-softwareproducts.com

                #8
                Re: Help with code snippet

                On Jul 9, 2:52 pm, Robert Bauck Hamar <roberth+n...@i fi.uio.nowrote:
                Christopher.E.S anb...@gmail.co m wrote:
                On Jul 9, 10:30 am, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
                wrote:
                cougr...@gmail. com wrote:
                I've got a take home final for my computer class and it has a bonus
                question concerning
                some programming that we didn't have time to cover. I've already got
                an A in the class, so
                it won't affect my grade one way or another, however my obsessive/
                compulsive tendencies
                just can't let it go. Could I get some help in explaining what this
                code snippet does;
                >
                char foo[8]=" bcd f\n\0";
                int i=2;
                >
                while (foo[i++]) putchar(foo[i]);
                >
                I don't understand all the descriptions that everyone has posted here.
                This code just appears to print cd f to standard out. This code is
                just
                trying to change directories.
                >char foo[8]=" bcd f\n\0";
                As for the syntax for the initialization for the array, it doesn't
                appear to be correct.

                That's my two cents...take it for what it is worth....

                Comment

                • Victor Bazarov

                  #9
                  Re: Help with code snippet

                  ajones@i-softwareproduct s.com wrote:
                  [..]
                  >>>> char foo[8]=" bcd f\n\0";
                  >>>> int i=2;
                  >>
                  >>>> while (foo[i++]) putchar(foo[i]);
                  >>
                  >
                  I don't understand all the descriptions that everyone has posted here.
                  This code just appears to print cd f to standard out. This code is
                  just
                  trying to change directories.
                  It's not trying to change anything except the contents of the standard
                  output.
                  [..]
                  V
                  --
                  Please remove capital 'A's when replying by e-mail
                  I do not respond to top-posted replies, please don't ask


                  Comment

                  • ajonesfl

                    #10
                    Re: Help with code snippet

                    On Jul 9, 4:58 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
                    ajo...@i-softwareproduct s.com wrote:
                    [..]
                    >>> char foo[8]=" bcd f\n\0";
                    >>> int i=2;
                    >
                    >>> while (foo[i++]) putchar(foo[i]);
                    >
                    I don't understand all the descriptions that everyone has posted here.
                    This code just appears to print cd f to standard out. This code is
                    just
                    trying to change directories.
                    >
                    It's not trying to change anything except the contents of the standard
                    output.
                    >
                    I stand corrected. It just prints cd f to the standard out, as long as
                    standard out has not been redefined.....
                    [..]
                    >
                    V
                    --
                    Please remove capital 'A's when replying by e-mail
                    I do not respond to top-posted replies, please don't ask

                    Comment

                    • Default User

                      #11
                      Re: Help with code snippet

                      Robert Bauck Hamar wrote:
                      char foo[8]=" bcd f\n\0";
                      From what I've been
                      able to glean from the book is that its declaring an array of 8
                      integers
                      >
                      Yes. The integers are of type char.
                      >
                      Then this array is initialised with nine elements, which is an error.
                      You are not allowed to initialise an array with more elements than it
                      can hold. Thus, the answer to your question: It does not compile.
                      This points out one of the subtle differences between C and C++. That
                      would be a valid initalization in C, as it would skip implictly adding
                      the automatic null terminator. One is explicitly specified, so it would
                      even be a legal C string.




                      Brian

                      Comment

                      • James Kanze

                        #12
                        Re: Help with code snippet

                        On Jul 9, 7:00 pm, cougr...@gmail. com wrote:
                        I've got a take home final for my computer class and it has a bonus
                        question concerning
                        some programming that we didn't have time to cover. I've already got
                        an A in the class, so
                        it won't affect my grade one way or another, however my obsessive/
                        compulsive tendencies
                        just can't let it go. Could I get some help in explaining what this
                        code snippet does;
                        char foo[8]=" bcd f\n\0";
                        int i=2;
                        while (foo[i++]) putchar(foo[i]);
                        It confuses the reader, and shows that 1) the author doesn't
                        know C or C++ very well, if at all (since the final \0 in the
                        string is superflu), and 2) doesn't care whether the code is
                        understandable or not (since he practices conciseness to the
                        point of obfuscation).

                        Presumably, what the author was trying to do was something like:
                        std::cout << "cd f" << std::endl ;
                        or:
                        std::string foo( "cd f\n" ) ;
                        std::cout << foo ;
                        Both of which are significantly easier to read.

                        I don't know the exact context of the question, but any course
                        which teaches you to write things like "while (foo[i++])" pretty
                        much makes you unemployable in a company which values quality
                        code.

                        --
                        James Kanze (GABI Software) email:james.kan ze@gmail.com
                        Conseils en informatique orientée objet/
                        Beratung in objektorientier ter Datenverarbeitu ng
                        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                        Comment

                        • =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

                          #13
                          Re: Help with code snippet

                          On 2007-07-09 23:07, ajonesfl wrote:
                          On Jul 9, 4:58 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
                          >ajo...@i-softwareproduct s.com wrote:
                          [..]
                          >>>> char foo[8]=" bcd f\n\0";
                          >>>> int i=2;
                          >>
                          >>>> while (foo[i++]) putchar(foo[i]);
                          >>
                          I don't understand all the descriptions that everyone has posted here.
                          This code just appears to print cd f to standard out. This code is
                          just
                          trying to change directories.
                          >>
                          >It's not trying to change anything except the contents of the standard
                          >output.
                          >>
                          >
                          I stand corrected. It just prints cd f to the standard out, as long as
                          standard out has not been redefined.....
                          Sorry, but wrong again. It will always print to standard out, regardless
                          whether it has been redirected or not. However you'll only see it in
                          your terminal if it has not been redirected, but that's of no concern
                          for the program.

                          --
                          Erik Wikström

                          Comment

                          • ajonesfl

                            #14
                            Re: Help with code snippet

                            Sorry, but wrong again. It will always print to standard out, regardless
                            whether it has been redirected or not. However you'll only see it in
                            your terminal if it has not been redirected, but that's of no concern
                            for the program.
                            >
                            --
                            Erik Wikström
                            Before you accuse me of being wrong. Please get your facts correct and
                            go reread my last post. I never said that it would not go to standard
                            out. Maybe I needed to qualify my statement. I assumed that most
                            people know that by default standard out is the terminal. You can
                            redirect standard out to be just about anything you want it to be.
                            Just because you redirect standard out to be a file does not change
                            that it is standard out. I did not think I had to explain that. Based
                            on the context of the post here no one was talking about anything but
                            terminal output.

                            So when I wrote:
                            It just prints cd f to the standard out, as long as
                            standard out has not been redefined.....
                            The assumption was that the chararcters would be printed to the
                            terminal vs. a file or socket.....

                            Adam Jones

                            Comment

                            Working...