473,224 Members | 1,375 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,224 software developers and data experts.

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

Similar topics

1
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
18
by: Vijay Kumar R Zanvar | last post by:
Hi c.l.c, Is this a correct method of allocating a 3-dimensional array? /* assume all malloc's succeed */ #define MAT 2 #define ROW 2 #define COL 2
5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
8
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.