Array of strings (char array) in C (Arduino). How do I accomplish it?

30,393

Solution 1

Just use a two dimensional array. Like:

char a[][]={
    "0000000000",
    "0000000000",
    "0011111100",
    "0000100100",
    "0000100100",
    "0011111100",
    "0000000000",
    "0000000000",
};

Solution 2

Based on your comment, I think this is what you are asking for:

const char* data_sets[][200] =
    {
        { "00000", "11111",         },
        { "22222", "33333", "44444" },
        { "55555"                   },
    };

Each entry in data_sets is an array of 200 const char*. For accessing:

for (size_t i = 0; i < sizeof(data_sets) / sizeof(data_sets[0]); i++)
{
    const char** data_set = data_sets[i];
    printf("data_set[%u]\n", i);
    for (size_t j = 0; data_set[j]; j++)
    {
        printf("  [%s]\n", data_set[j]);
    }
}

See online demo at http://ideone.com/6kq2M.

Share:
30,393
z3cko
Author by

z3cko

ruby, rails, osx, mongodb, unix. trying to get my head around cocoa developing. pro web developer and technology researcher.

Updated on October 18, 2020

Comments

  • z3cko
    z3cko over 3 years

    I want to access certain data which basically looks like this:

    char* a[]={
        "0000000000",
        "0000000000",
        "0011111100",
        "0000100100",
        "0000100100",
        "0011111100",
        "0000000000",
        "0000000000",
    };
    

    I have around 200 of those data sets and want to access it in the way.

    fooBar[23]; --> This should return the 23rd character array (which looks like the example listed above).

    As far as I understand from my other programming knowledge, I would need an array of Strings. The array index is my lookup number (which will be a maximum of 255). The array values are the character arrays as shown above.

    How can this be accomplished with C (Arduino IDE)?