char array question

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
 
D

David Theese

ali said:
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'.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

ali said:
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.
 
A

Alf P. Steinbach

* 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;
}
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::eek:stream_iterator
#include <algorithm> // std::copy

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

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

Ali Cehreli

'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
 
M

Mabden

ali said:
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.
 
J

johnny

ali said:
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'
 
J

John Harrison

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
 
J

josh

David said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top