By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Beginner: A question about strings/arrays

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.