Beginner: A question about strings/arrays

T

Tarjei Romtveit

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
 
K

Kai-Uwe Bux

Tarjei said:
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
 
T

Tim Love

Tarjei Romtveit said:
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.
 
L

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
 
T

Tarjei Romtveit

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*
 
J

Jim Langston

Tarjei Romtveit said:
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top