2

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)?

3
  • 2
    You've already shown the solution to your question. So what is unclear about that? Commented Oct 17, 2012 at 9:32
  • Note that fooBar[23] is actually the 24th character array. I know nothing about arduino and what restrictions it places on the use of C but the proposed array is correct (maybe change type to const char* as string literals should not be modified). Commented Oct 17, 2012 at 9:37
  • to make it more clear: i need an array of the example array construct. Commented Oct 17, 2012 at 9:46

2 Answers 2

4

Just use a two dimensional array. Like:

char a[][]={
    "0000000000",
    "0000000000",
    "0011111100",
    "0000100100",
    "0000100100",
    "0011111100",
    "0000000000",
    "0000000000",
};
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.