473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

char array question

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
8 2727

"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
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
* 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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
21
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. ...
6
by: Sona | last post by:
Hi, What's the advantage/disadvantage of using a "const char*" over a "char*" ? I read some place that char* are string literals that on some machines are stored in a read only memory and cannot...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
16
by: MN | last post by:
I have a question : How to understand the mean of char** type ?
5
by: codeGhost | last post by:
I've been trying to ignore this issue for a while now, but I've come to the point in my code where I can't do so anymore. (For those of you who are wondering, this is NOT a homework question). ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.