468,513 Members | 1,597 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Array of Char

Hi,

How to understand the difference between the following three.

My understanding is the number in bracket minus one is the max number
of chars to store in the char array , right?
Thanks in advance.

Program 1
#include <iostream>
using namespace std;

int main ()
{

char x[5]={'a','b','c','d','e'};
cout<<x;
}

Program 2
#include <iostream>
using namespace std;

int main ()
{

char x[5]={'a','b','c','d''};
cout<<x;
}

Program 3
#include <iostream>
using namespace std;

int main ()
{

char x[5]="abcde";
cout<<x;
}

Aug 1 '06 #1
2 7414
Michael wrote:
How to understand the difference between the following three.

My understanding is the number in bracket minus one is the max number
of chars to store in the char array , right?
The confusion is apparently between "an array of char" and "a C string".
The former just stores those chars. The latter stores chars _and_ is
terminated with a char with the value 0 (null char), which also has to
be stored in the array.
Program 1
#include <iostream>
using namespace std;

int main ()
{

char x[5]={'a','b','c','d','e'};
cout<<x;
When you use the name of the array like that ('x'), it is converted
to a pointer to its first element (zeroth element, actually). Then,
it is treated as a C string. 'cout' is looking for the terminating
null char. Your array doesn't have any. That means outputting it
like that causes undefined behaviour.
}

Program 2
#include <iostream>
using namespace std;

int main ()
{

char x[5]={'a','b','c','d''};
You apparently have an extra single-quote char, probably because you
didn't copy-and-paste your code, but typed it in. OK, we'll consider
it a typo. Now, what's happening here? You declared the array to
have 5 elements and only provided 4 initialisers. All the elements
after the last initialiser are zero-initialised. So, after the four
characters in your 'x' array you have a *null char*.
cout<<x;
Outputting the array of char like that is *absolutely fine*, since it
is a true C string (a bunch of chars terminated with a null char).
}

Program 3
#include <iostream>
using namespace std;

int main ()
{

char x[5]="abcde";
I believe this is a syntax error. You provided 6- (yes, six-) char
literal as the initialiser for a 5-element array. That's too many
initialisers. Your compiler should refuse to compile that code.

The most common way to do what you seem to want here is this:

char x[] = "abcde";

which will make the size of that array 6 (the number of chars in
the initialiser literal, which contains the [hidden] null char at
the end).
cout<<x;
}
HTH

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #2

Victor Bazarov wrote:
Michael wrote:
[snip]
char x[5]="abcde";

I believe this is a syntax error. You provided 6- (yes, six-) char
literal as the initialiser for a 5-element array. That's too many
initialisers. Your compiler should refuse to compile that code.
[snip]

Yes, in C++ it is an erroneous statement.
(Unfortunately) In C it is a legal statement.

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Aug 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Vijay Kumar R Zanvar | last post: by
5 posts views Thread by pandapower | last post: by
4 posts views Thread by Simon Schaap | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
24 posts views Thread by Michael | last post: by
8 posts views Thread by isaac2004 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.