473,387 Members | 1,664 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,387 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 7641
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.