Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help with code snippet

Reply
Thread Tools

Help with code snippet

 
 
cougre.j@gmail.com
Guest
Posts: n/a
 
      07-09-2007
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

 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-09-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Christopher.E.Sanborn@gmail.com
Guest
Posts: n/a
 
      07-09-2007
On Jul 9, 10:30 am, Robert Bauck Hamar <(E-Mail Removed)>
wrote:
> (E-Mail Removed) 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?

 
Reply With Quote
 
cringecoder@gmail.com
Guest
Posts: n/a
 
      07-09-2007
On Jul 9, 1:00 pm, (E-Mail Removed) 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

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      07-09-2007
(E-Mail Removed) wrote:
> On Jul 9, 1:00 pm, (E-Mail Removed) 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-09-2007
(E-Mail Removed) 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?
 
Reply With Quote
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-09-2007
(E-Mail Removed) wrote:

> On Jul 9, 10:30 am, Robert Bauck Hamar <(E-Mail Removed)>
> wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
ajones@i-softwareproducts.com
Guest
Posts: n/a
 
      07-09-2007
On Jul 9, 2:52 pm, Robert Bauck Hamar <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Jul 9, 10:30 am, Robert Bauck Hamar <(E-Mail Removed)>
> > wrote:
> >> (E-Mail Removed) 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....

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-09-2007
(E-Mail Removed) 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


 
Reply With Quote
 
ajonesfl
Guest
Posts: n/a
 
      07-09-2007
On Jul 9, 4:58 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help - can anyone tell me how to get this code snippet to work lawrencef@hlac.com.au Javascript 5 05-08-2008 06:28 PM
Code snippets, tool to convert basic to .snippet xml? Edwin Knoppert ASP .Net 0 11-30-2005 03:45 PM
Is there a code snippet showing how to do a Redirect on a TreeView event Tom ASP .Net 3 12-01-2004 03:04 AM
How can I save my code snippet on toolbox in vs.net? Clare Hsiao ASP .Net 0 02-12-2004 02:01 AM
Help!-Errors while running this snippet code Kavitha Rao C Programming 2 07-03-2003 09:15 AM



Advertisments