Thread: Character Arrays

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    Character Arrays

    I am new here and I have a problem. It may have been discused in one of the 16911 other threads but I have limited time to search for it currently. My problem is that when I type the following I get an error:
    Code:
    int main
    {
        char bob[9];
        bob = "hello";
    }
    I have tried variations on this and the only thing that works is:

    Code:
    int main
    {
        char bob[9];
        bob[0] = 'h';
        bob[1] = 'e';
        bob[2] = 'l';
        bob[3] = 'l';
        bob[4] = 'o';
    }
    It may be that it is a Dev problem if so please tell me, I am getting tired of all the errors its causing when I forget
    Last edited by Smiley27; 04-12-2004 at 03:23 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Use / not \ in code tags

    And its
    char bob[9] = "hello";

    or
    strcpy( bob, "hello" );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you are trying to assign a string literal to a null terminated char array, but the assignment operator isn't assigned for that task. you could do this:

    char bob[9];
    cout << "enter a word of under nine characters" << endl;
    cin >> bob;
    cout << "you entered " << bob << endl;

    our you could do this:

    char bob[9];
    strcpy(bob, "hello");
    cout << bob;

    strcpy and other functions to manipulate C style strings (aka null terminated char arrays) are available in cstring or string.h header files, whichever is available with your compiler.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    strcpy should work, I tried
    char bob[9] = "hello";
    and it didn't work. Also, getting it from cin is not what I want because it is going to be a text game and the variable I want set I don't want the user to set.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    How about posting a complete program which shows the problem (as you see it)
    One line snippets taken way out of context just don't cut it.

    There is nothing inherently wrong with
    char bob[9] = "hello";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    how about creating a pointer to a string
    Code:
    char *str= "smith";
    
         OR
    
    char *str;
    
    cout << "enter new name";
    cin >> str;

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how about creating a pointer to a string
    How about not?

    >char *str= "smith";
    This is actually a deprecated feature in C++. str should be a const char * to be fully correct, not a char *.

    >char *str;
    >cin >> str;
    str doesn't point to any memory you own, so trying to store data at that address is a Very Bad Thing.
    My best code is written with the delete key.

  8. #8
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    What's wrong with the following?


    Code:
    char *str[] = { "hiya" };

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What's wrong with the following?
    >char *str[] = { "hiya" };
    The same as above. You just hid it behind the declaration of an array of size 1.
    My best code is written with the delete key.

  10. #10
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    I've read quite a few books that follow the ANSI standard and they teach it this way. I understand why you should make it const though.

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Salem, its not the whole program because I get the same error in all of my programs. I think its just my compiler.

    Also strcpy worked fine.

  12. #12
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    Sorry if this seems like a silly question....

    Why the const? What is this going to do and why is it important?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Until Smiley27 actually gets around to posting a complete program which can be compiled - with whatever problem is present, further discussion is moot
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169
    how can you get
    Code:
    char bob[5] = "hello";
    to compile tho? i always have to use strcpy.
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Because in C++, you cannot silently drop the \0 from a string initaliser (but you can in C)
    Normally, you don't specify a size for the array, you let the compiler do it
    char bob[] = "hello";
    and the whole problem goes away

    Besides,
    char bob[5];
    strcpy( bob, "hello" );
    is a buffer overflow, so you're really much worse off despite your apparent progress.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM
  5. Spaces in Character Arrays
    By ADLOTS in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 04:24 AM