By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

char array question

P: n/a
ali
Hi,

I'm having a problem understanding the reason for output on the
following code:

#include <iostream.h>

int main()
{

char array[]={'w','e','l','c','o','m','e'};

cout<<array<<endl;

return 0;
}

When i run this, the output is something like this:

welcome¶+*?

Will appreciate some help on understanding the concept.

Thanks,

ali
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"ali" <tj@raha.com> wrote in message
news:t3********************************@4ax.com...
Hi,

I'm having a problem understanding the reason for output on the
following code:

#include <iostream.h>

int main()
{

char array[]={'w','e','l','c','o','m','e'};

cout<<array<<endl;

return 0;
}

When i run this, the output is something like this:

welcome¶+ ?

Will appreciate some help on understanding the concept.

Thanks,

ali


Strings require a null terminator, which you are missing. This is character
'\0'.
Jul 22 '05 #2

P: n/a
ali wrote:
char array[]={'w','e','l','c','o','m','e'};

cout<<array<<endl;

return 0;
}

When i run this, the output is something like this:

welcome?+ ?

Will appreciate some help on understanding the concept.


C-style strings are array of chars zero terminated. You have an array of
char that is not zero terminated. "cout <<" when passed an array of char
supposes that it is a c-style string, the in that case it continues writing
anything has in memory after the array, interpreted as chars, until a 0 is
found.

--
Salu2
Jul 22 '05 #3

P: n/a
* ali:

#include <iostream.h>
Should be
#include <iostream>
'iostream.h' is not a standard C++ header. It was common before
standardization in 1996 (or 1997, depending on one's point of view).
int main()
{

char array[]={'w','e','l','c','o','m','e'};
Preferably make that an array of 'const' characters unless you mean
to change the contents, i.e.

char const array[] = { 'w','e','l','c','o','m','e' };

cout<<array<<endl;
std::cout << array << std::endl;
return 0;
}

When i run this, the output is something like this:

welcome¶+*?

Will appreciate some help on understanding the concept.


That's because operator<< assumes a character array is a sequence
of characters terminated by a zero byte, '\0'. You don't have a
zero byte at the end. So operator<< continues writing characters
(garbage memory content) until by chance it encounters a zero byte.

You could do it this way:
for( unsigned i = 0; i < sizeof(array); ++i )
{
std::cout << array[i];
}
std::cout << std::endl;
or, leveraging support in the standard library, but less easy to
understand what happens behind the scenes,
#include <iostream> // std::cout
#include <iterator> // std::ostream_iterator
#include <algorithm> // std::copy

int main()
{
char const array[] = { 'w','e','l','c','o','m','e' };

std::copy(
array, array + sizeof( array ),
std::ostream_iterator<char>( std::cout )
);
std::cout << std::endl;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4

P: n/a
On Fri, 06 Aug 2004 16:02:27 -0700, Alf P. Steinbach wrote:
'iostream.h' is not a standard C++ header. It was common before
standardization in 1996 (or 1997, depending on one's point of view).


How about 1998? :) The first edition of the standard document is dated
1998-09-01.
I remembered the subject of the comp.lang.c++.moderated post that broke
the news to the C++ community: "We have a standard!". Even though I found
the tail end of that thread (posted around August 1998), on
groups.google.com, the original message is missing.

Ali
Jul 22 '05 #5

P: n/a
"ali" <tj@raha.com> wrote in message
news:t3********************************@4ax.com...
Hi,

I'm having a problem understanding the reason for output on the
following code:

#include <iostream.h>

int main()
{

char array[]={'w','e','l','c','o','m','e'};

cout<<array<<endl;

return 0;
}

When i run this, the output is something like this:

welcome¶+ ?


In addition to what everyone else will tell you, I'd like to add that C
strings must be terminated by a null ('\0'). Just in case that message
doesn't get through.

--
Mabden\0

Jul 22 '05 #6

P: n/a
ali wrote:
Hi,

I'm having a problem understanding the reason for output on the
following code:

#include <iostream.h>

int main()
{

char array[]={'w','e','l','c','o','m','e'};

cout<<array<<endl;

return 0;
}

When i run this, the output is something like this:

welcome?+*?

Will appreciate some help on understanding the concept.

Thanks,

ali


You're missing the terminating null character '\0'

Jul 22 '05 #7

P: n/a
On Fri, 06 Aug 2004 18:42:55 -0400, ali <tj@raha.com> wrote:
Hi,

I'm having a problem understanding the reason for output on the
following code:

#include <iostream.h>

int main()
{

char array[]={'w','e','l','c','o','m','e'};

cout<<array<<endl;

return 0;
}

When i run this, the output is something like this:

welcome¬¶+¬*?

Will appreciate some help on understanding the concept.

Thanks,

ali


Change your array to this

char array[]={'w','e','l','c','o','m','e','\0'};

or this

char array[]="welcome";

Those are two different ways to add the nul terminateor that everyoe else
has told you about.

john
Jul 22 '05 #8

P: n/a
David Theese wrote:
Strings require a null terminator, which you are missing. This is character
'\0'.


But note that \0 is not a special escape sequence for nul, it's just an
ordinary octal escape sequence with only one digit. This can make a
difference for the rare case of inserting a nul in the middle of a
string constant.

"\087" is four characters but "\076" is only two.

-josh

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.