Help with code snippet

Discussion in 'C++' started by cougre.j@gmail.com, Jul 9, 2007.

  1. Guest

    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);

    Thanks in advance,
    Cougar
     
    , Jul 9, 2007
    #1
    1. Advertising

  2. 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);


    Why don't you try it?

    --
    rbh
     
    Robert Bauck Hamar, Jul 9, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 9, 10:30 am, Robert Bauck Hamar <>
    wrote:
    > 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);

    >
    > 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?
     
    , Jul 9, 2007
    #3
  4. Guest

    On Jul 9, 1:00 pm, 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);
    >
    > 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);

    Is essentially the same as...

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

    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
     
    , Jul 9, 2007
    #4
  5. Marcus Kwok Guest

    wrote:
    > On Jul 9, 1:00 pm, 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
     
    Marcus Kwok, Jul 9, 2007
    #5
  6. 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?
     
    Juha Nieminen, Jul 9, 2007
    #6
  7. wrote:

    > On Jul 9, 10:30 am, Robert Bauck Hamar <>
    > wrote:
    >> 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);

    >>
    >> 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);
    }

    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);

    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
     
    Robert Bauck Hamar, Jul 9, 2007
    #7
  8. Guest

    On Jul 9, 2:52 pm, Robert Bauck Hamar <> wrote:
    > wrote:
    > > On Jul 9, 10:30 am, Robert Bauck Hamar <>
    > > wrote:
    > >> 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 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....
     
    , Jul 9, 2007
    #8
  9. wrote:
    > [..]
    >>>>> char foo[8]=" bcd f\n\0";
    >>>>> int i=2;

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

    >>

    >
    > 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
     
    Victor Bazarov, Jul 9, 2007
    #9
  10. ajonesfl Guest

    On Jul 9, 4:58 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > [..]
    > >>>>> char foo[8]=" bcd f\n\0";
    > >>>>> int i=2;

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

    >
    > > 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
     
    ajonesfl, Jul 9, 2007
    #10
  11. Default User Guest

    Robert Bauck Hamar wrote:

    > 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
     
    Default User, Jul 9, 2007
    #11
  12. James Kanze Guest

    On Jul 9, 7:00 pm, 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);


    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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 10, 2007
    #12
  13. On 2007-07-09 23:07, ajonesfl wrote:
    > On Jul 9, 4:58 pm, "Victor Bazarov" <> wrote:
    >> wrote:
    >> > [..]
    >> >>>>> char foo[8]=" bcd f\n\0";
    >> >>>>> int i=2;

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

    >>
    >> > 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 10, 2007
    #13
  14. ajonesfl Guest

    > 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
     
    ajonesfl, Jul 11, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clare Hsiao
    Replies:
    0
    Views:
    331
    Clare Hsiao
    Feb 12, 2004
  2. Tom
    Replies:
    3
    Views:
    425
    Jeffrey Palermo [MCP]
    Dec 1, 2004
  3. Edwin Knoppert
    Replies:
    0
    Views:
    372
    Edwin Knoppert
    Nov 30, 2005
  4. Kavitha Rao

    Help!-Errors while running this snippet code

    Kavitha Rao, Jul 2, 2003, in forum: C Programming
    Replies:
    2
    Views:
    740
    Kavitha Rao
    Jul 3, 2003
  5. Replies:
    5
    Views:
    98
    apatheticagnostic
    May 8, 2008
Loading...

Share This Page