473,387 Members | 1,492 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.

Beginner: A question about strings/arrays

I'm still a newbie into C++ programming, so I got a quite foolish
string related question.

Using: Dev-cpp 4.9.9.2 (I think Dev-Cpp uses a gcc compiler of some
sort)
If i declare a char string like this:

char szString[5] = "Hello";

The content of the szString array is now somthing similar to this:
szString[0] == H, szString[1] == e.. and so on, until szString[5] ==
\0. (right?)

But my compiler returns a following error:

"initializer-string for array of chars is too long"

If i do the declartaion like this

char szString[6] = "Hello";

it compiles correctly. If I choose to write something on the screen
like:

cout << szString[6] << endl;

It outputs a strange symbol that often differs (Probably something in
the memory).

The 'cout << szString << endl;' ouputs Hello.

Is there something wrong with my compiler? Or is my logical talent
really that bad?

(I'm a beginner so bear with me)
Mvh

Tarjei

Aug 21 '05 #1
5 11387
Tarjei Romtveit wrote:
I'm still a newbie into C++ programming, so I got a quite foolish
string related question.

Using: Dev-cpp 4.9.9.2 (I think Dev-Cpp uses a gcc compiler of some
sort)
If i declare a char string like this:

char szString[5] = "Hello";

The content of the szString array is now somthing similar to this:
szString[0] == H, szString[1] == e.. and so on, until szString[5] ==
\0. (right?)

But my compiler returns a following error:

"initializer-string for array of chars is too long"

If i do the declartaion like this

char szString[6] = "Hello";

it compiles correctly. If I choose to write something on the screen
like:

cout << szString[6] << endl;

It outputs a strange symbol that often differs (Probably something in
the memory).

The 'cout << szString << endl;' ouputs Hello.

Is there something wrong with my compiler? Or is my logical talent
really that bad?


Your compiler is just a little bit better at counting than you:

szString[0] = 'H'
szString[1] = 'e'
szString[2] = 'l'
szString[3] = 'l'
szString[4] = 'o'
szString[5] = \0

Your compiler thinks that this is 6 lines, now you asked the compiler to
provide an array of 5 characters. The compiler thinks 6 > 5 and complains.

Generally, T[n] is an array of n objects numbered from 0 to (n-1).
Best

Kai-Uwe Bux
Aug 21 '05 #2
"Tarjei Romtveit" <ta*************@gmail.com> writes:
If i declare a char string like this: char szString[5] = "Hello"; The content of the szString array is now somthing similar to this:
szString[0] == H, szString[1] == e.. and so on, until szString[5] ==
\0. (right?) Trouble is, you need 6 characters to store Hello\0, but you've only
asked for 5 characters.
char szString[6] = "Hello";
This is ok.

If I choose to write something on the screen
like: cout << szString[6] << endl;


This prints the 7th character of the szString array, an array that
only has 6 characters.

Aug 21 '05 #3
LR
Tarjei Romtveit wrote:

If i declare a char string like this:

char szString[5] = "Hello";

The content of the szString array is now somthing similar to this:
szString[0] == H, szString[1] == e.. and so on, until szString[5] ==
\0. (right?)
Close, but no.

char s[5];

will make an array of char with five (5) elements. The first index is
zero (0) and the last index is four (4). 0, 1, 2, 3, and 4 are five
seperate digits.

"Hello" including the final trailing '/0' is six chars. So you're off by
one.

But my compiler returns a following error:

"initializer-string for array of chars is too long"

If i do the declartaion like this

char szString[6] = "Hello";

it compiles correctly.
Sure.

char s[6] = "Hello";

Six elements in the array named s. Valid indicies inclusive range from
zero to five.

Maybe better to write:

const char szString[] = "Hello";

Because I do so hate to count the number of characters in a string. I
often get it wrong and it just leads to headaches later should I want
the string to contain something else like "Goodbye".

or even better, perhaps,

#include <string>
..
..
..
const std::string sHello = "Hello";
If I choose to write something on the screen
like:

cout << szString[6] << endl;

It outputs a strange symbol that often differs (Probably something in
the memory).
Because char x[6] has six valid indicies inclusive 0 to 5. 6 is one past
the last valid index. Who knows what's there?

Try this little bit of code:
----------
#include <iostream>
int main() {
std::cout << sizeof("Hello") << std::endl;
}
----------


The 'cout << szString << endl;' ouputs Hello.

Is there something wrong with my compiler?
It's hard to tell from just this simple case. Continue with your
testing and you're bound to eventually find a bug. ;)

Or is my logical talent
really that bad?

(I'm a beginner so bear with me)


For some people the idea that arrays in C++ have zero as their first
index can be a bit of a stumbling block. Get up, dust yourself off and
carry on.

BTW, if you don't mind, could you tell us what book you're using? Or are
you taking a course?

LR
Aug 21 '05 #4
Oki, Thanks alot. As I thought, a simple solution.

In this case I think there has been a misunderstanding between me and
the teacher of the course I'm taking. (It isnt always easy when your
native language isnt english, and the teacher is from Wales or
something)*blush*

Aug 21 '05 #5

"Tarjei Romtveit" <ta*************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I'm still a newbie into C++ programming, so I got a quite foolish
string related question.

Using: Dev-cpp 4.9.9.2 (I think Dev-Cpp uses a gcc compiler of some
sort)
If i declare a char string like this:

char szString[5] = "Hello";

The content of the szString array is now somthing similar to this:
szString[0] == H, szString[1] == e.. and so on, until szString[5] ==
\0. (right?)

But my compiler returns a following error:

"initializer-string for array of chars is too long"

If i do the declartaion like this

char szString[6] = "Hello";

it compiles correctly. If I choose to write something on the screen
like:

cout << szString[6] << endl;

It outputs a strange symbol that often differs (Probably something in
the memory).

The 'cout << szString << endl;' ouputs Hello.

Is there something wrong with my compiler? Or is my logical talent
really that bad?

(I'm a beginner so bear with me)
Mvh

Tarjei


1. Arrays in C/C++ start at 0, not 1. The 6th element of the array is [5],
not [6].
2. You need to allocate one byte for the null terminator.

"Hello" actually takes up 6 bytes. 'H' 'e' 'l' 'l' 'o' '\0'. In an array
they wold be shown as [0] to [5]
Aug 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
7
by: Sonoman | last post by:
I am trying to do this: cin >> temp; if (temp == "n"){ Then do something... } temp was declared as a string and the input I give at the prompt is n, but it skips the condition for the if...
2
by: Riaan C | last post by:
Hi I'm new to C. Here's the problem. I understand the whole concept of pointers and can easily use it with normal non-array variables. I want to declare a array of strings, thus...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
1
by: Bill Maher | last post by:
I have an array that I use to write to the file on the C:\ drive. I want to print the file. How can I do it. Here is my code for the write to the file: I'm using "writeline". To allow for...
33
by: aaron | last post by:
I have a question in my class.. hoping to get some help I need to create a program that will print firstName middleName lastName then their initials User will input: John Smith Doe Output:...
13
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top ...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...
0
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,...

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.