Beginner: A question about strings/arrays

Discussion in 'C++' started by Tarjei Romtveit, Aug 21, 2005.

  1. 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
     
    Tarjei Romtveit, Aug 21, 2005
    #1
    1. Advertising

  2. Tarjei Romtveit

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Aug 21, 2005
    #2
    1. Advertising

  3. Tarjei Romtveit

    Tim Love Guest

    "Tarjei Romtveit" <> 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.
     
    Tim Love, Aug 21, 2005
    #3
  4. Tarjei Romtveit

    LR Guest

    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
     
    LR, Aug 21, 2005
    #4
  5. 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*
     
    Tarjei Romtveit, Aug 21, 2005
    #5
  6. Tarjei Romtveit

    Jim Langston Guest

    "Tarjei Romtveit" <> wrote in message
    news:...
    > 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]
     
    Jim Langston, Aug 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    624
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    1,013
    Mabden
    Sep 2, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    789
    Malcolm
    Jun 24, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,160
    Philipp
    Jan 20, 2009
  5. Replies:
    2
    Views:
    103
    Harold Hausman
    Nov 13, 2006
Loading...

Share This Page