473,569 Members | 2,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7649
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
2058
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 data-types of my choosing (eg an array of char, short, or int). The application has to do with generating checksum-type values for files or strings,...
58
10077
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 code... TCHAR myArray; DoStuff(myArray);
18
6423
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
5621
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 1. char array or char array and it will get decayed to 2. char *array
3
2849
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 call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); ...
4
8788
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below...
12
5492
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 following program: #include<stdio.h> #define SIZE 1 int main()
204
12940
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 = {0,1,2,4,9};
24
3413
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
8
1873
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 file into a 2d array. I have this to get the file. #include <iostream> #include <string> #include <fstream> using namespace std;
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
7618
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
7926
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. ...
1
7679
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
6287
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
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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

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.