Thread: How to select a string from an arry of strings

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    How to select a string from an arry of strings

    I have defined the following:

    Code:
    static volatile char const *name_list[8][8] = {"Line 0  ", "Line 1  ", "Line 2  ", "Line 3  ", "Line 4  ", "Line 5  ", "Line 6  ", "Line 7", "Line 8  "};

    My question is: how do I select a string from this array and in what kind of variable can I store the selected string?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by MNM(tm) View Post
    I have defined the following:

    Code:
    static volatile char const *name_list[8][8] = {"Line 0  ", "Line 1  ", "Line 2  ", "Line 3  ", "Line 4  ", "Line 5  ", "Line 6  ", "Line 7", "Line 8  "};

    My question is: how do I select a string from this array and in what kind of variable can I store the selected string?
    Use an index, or a pointer, to select your string. You only need *name_list[8]], (or name_list[8][8]), but not both, for your array.

    what would this do?
    Code:
    printf("%s \n", name_list[2]);
    strings are most commonly stored in char arrays in C. What is the problem with using one, here?
    Last edited by Adak; 12-31-2011 at 07:47 AM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Quote Originally Posted by Adak View Post
    You only need *name_list[8], (or name_list[8][8]).
    In both cases I get a compiler error: "Too many initializer values", so that's why I have to use "char const *name_list[8][8] ..."

    But when I also define another variable:
    Code:
    static volatile char *selected_name;
    and I try to assign a selected string form the array to it, by using:

    Code:
    selected_name = name_list[8];
    the compiler complains with an error: "expression must be a modifiable lvalue"



    ps.: "char const name_list[][8] ..." seems to be accepted by the compiler as well.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    You can not use the assignment operator with C-strings, you need to use strcpy().

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Humbug.

    strcpy() needs string.h to be included, and that's not available in my compiler. (I'm using TI's Code Composer Studio to write programs for MSP430 processors).

    I guess I'm going to need to use pointers then?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    Or a loop to copy character by character until the end of string character. Are you sure that this compiler does not include string.h?

    Jim

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Yup, I'm sure.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well of course, 0 to 8 strings, is NINE strings, so there's the cause of your "too many initializers" error.

    Writing you own strcpy() is easy:

    Code:
    for(i=0;origin_array[i];i++) {
       destination_array[i]=origin_array[i];
    }
    destination_array[i]='\0';

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Maybe you just need to add a const like this:
    Code:
    static volatile const char *selected_name;
    Why are you using volatile ?

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You could generate it programatically, and then reverse it to get the strings back out again:

    Code:
        register u8 i;
        char **strs;
        strs = kmalloc(8,GFP_KERNEL);
        
        for (i = 0; i < 8; i++) {
            strs[i] = kmalloc(8,GFP_KERNEL);
            strs[i][0] = 'L';
            strs[i][1] = 'i';
            strs[i][2] = 'n';
            strs[i][3] = 'e';
            strs[i][4] = ' ';
            strs[i][5] = i + '0';
        }

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Quote Originally Posted by oogabooga View Post
    Maybe you just need to add a const like this:
    Code:
    static volatile const char *selected_name;
    Why are you using volatile ?
    To make sure the compiler creates the const in RAM instead of Flash Memory.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    3,773
    Quote Originally Posted by MNM(tm) View Post
    To make sure the compiler creates the const in RAM instead of Flash Memory.
    IIRC, volatile does NOT mean it does what you want.

    It means the value can change without code changing it; it tells the compiler NOT to optimize the variable out.

    Tim S.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Okay, then I was making a terribly wrong assumption.
    Thanks for making this clear to me.

  14. #14
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Just curious, why wouldn't your compiler include string.h? It's apart of the standard C library, and all of those headers should be installed by default.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-07-2010, 03:57 PM
  2. about arry help!!!!
    By jaxlle in forum C Programming
    Replies: 1
    Last Post: 11-09-2006, 05:11 PM
  3. Assigning Variable to arry
    By Vicked in forum Windows Programming
    Replies: 3
    Last Post: 04-13-2003, 08:09 PM
  4. How to set default value in arry
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 01:58 PM
  5. arry/character handling
    By itld in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2002, 03:16 PM