Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Beginner: A question about strings/arrays

Reply
Thread Tools

Beginner: A question about strings/arrays

 
 
Tarjei Romtveit
Guest
Posts: n/a
 
      08-21-2005
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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-21-2005
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
 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      08-21-2005
"Tarjei Romtveit" <(E-Mail Removed)> 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.

 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      08-21-2005
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
 
Reply With Quote
 
Tarjei Romtveit
Guest
Posts: n/a
 
      08-21-2005
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*

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      08-21-2005

"Tarjei Romtveit" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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]


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments