473,569 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<en dl;

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 2737

"ali" <tj@raha.com> wrote in message
news:t3******** *************** *********@4ax.c om...
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<<en dl;

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<<en dl;

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<<en dl;
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_it erator
#include <algorithm> // std::copy

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

std::copy(
array, array + sizeof( array ),
std::ostream_it erator<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++.m oderated 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.c om, the original message is missing.

Ali
Jul 22 '05 #5
"ali" <tj@raha.com> wrote in message
news:t3******** *************** *********@4ax.c om...
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<<en dl;

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<<en dl;

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<<en dl;

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
20929
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
8
6165
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 calling-function for further handling. If I've confused you, here's the code snippet (it's within a simple Win32-Dll-project): char* buffer; //...
21
18837
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. However the declaration: int main(int argc, char** argv) is common.
6
4742
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 be modified... does that apply on const char*? thanks Sona
5
3947
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 variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with...
18
26677
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
2245
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
3233
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
1637
by: MN | last post by:
I have a question : How to understand the mean of char** type ?
5
9849
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). Platform: VC++ 2002 System: Windows XP, sp3 ::First, here's the code in question:: char * cSection (const char* data, int start, int finish) {
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.