Thread: 2d array of strings

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

    Post 2d array of strings

    I have made a 2d array of char so simulate an Inventory

    char inventory[5][10]; //5 slots with a string of 10 chars each

    //I can even initialize this is 5 different items IE {"shirt", "pants",etc}

    The problem I have is I can't edit any individual part.

    inventory[1] = "shoe"// this doesn't work.

    Anyone have a suggestion?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can't assign arrays like that. I'm actually not even sure your second example of initializing the strings should work. But inventory[1] doesn't refer to anything. You should do this:

    Code:
    inventory[1][0] = 's';
    inventory[1][1] = 'h';
    inventory[1][2] = 'o';
    inventory[1][3] = 'e';

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Better
    strcpy( inventory[1], "shoe" );
    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.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or you can use an array of pointers instead of a 2d array:
    Code:
    char *inventory[5];
    
    inventory[0] = "shoe";
    inventory[1] = "C Programming Bible";
    ...and so forth...
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    Thumbs up

    Quote Originally Posted by itsme86
    Or you can use an array of pointers instead of a 2d array:
    Code:
    char *inventory[5];
    
    inventory[0] = "shoe";
    inventory[1] = "C Programming Bible";
    ...and so forth...

    That worked great, thanks
    I'm a little confused as to where the strings are stored if not in an array of char, but it works.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by localrob
    That worked great, thanks
    I'm a little confused as to where the strings are stored if not in an array of char, but it works.
    They are string literals, stored in a string table, unmodifiable by you, and if you change the pointer, lost to you. Example:
    Code:
    char *s = "foo";
    s = "bar";
    Since s is simply a pointer, non-constant, we're allowed to change where it points here. It first points to the string literal "foo. We then change what it points to, by making it point at another string literal, "bar". The first literal is now lost to us, and we have no way of finding it again, because we have nothing pointing at it any more.

    While we cannot also modify either of the literals. They're likely to be stored in a read-only portion of the program, and as such, by standard, are not allowed to modify them. For instance, we cannot do:
    Code:
    s = "bar";
    *s = 'c';
    This will, or should, generate a runtime error for you. You're not allowed to modify them.

    Basicly, string literals, at compile time, are all "seen" by the compiler, and stored in a table of strings which are then pointed to by whatever is trying to use them. Sort of like:
    Code:
    char *stringtable[] =
    {
        "this is a literal",
        "this is another",
        "same here",
    };
    
    ...
    void foo( void )
    {
        char *s = stringtable[2];
        printf("%s", s; );
    }
    It sort of ends up like that.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    3
    that clears it up, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  2. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  3. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Comparing a 2d Array (newbie)
    By Cockney in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2001, 12:15 PM