0

I have arrays of strings. I want to put these arrays into an array. How can I do so? I have tried this:

char const * const fruits[3] = {
    "apple",
    "banana",
    "orange",
};

char const * const colors[3] = {
    "red",
    "green",
    "blue",
};

char * preset_configurations[3] =
{
    NULL, /* leave the first one blank so that this list is 1-based */
    &fruits,
    &colors,
};

but I get warning: initialization from incompatible pointer type. Any idea?

boltup_im_coding
  • 6,345
  • 6
  • 40
  • 52
  • 1
    Don't put the ampersand in front of fruits and colors. Since they are arrays, they will be interpreted as pointers to their first elements in that situation. – ooga Jan 31 '14 at 23:27

2 Answers2

3

You need a double pointer and some consts (as well as getting rid of the ampersands) :

char const * const * preset_configurations[3] =
{
    NULL, /* leave the first one blank so that this list is 1-based */
    fruits,
    colors
};

EDIT: I suppose, given the extra information after I posted the above, that the best solution to your problem is:

// This will copy the characters of the words into the 3 16-byte arrays.
char fruits[3][16] = {
    "apple",
    "banana",
    "orange"
};
// Ditto.
char colors[3][16] = {
    "red",
    "green",
    "blue"
};
// This is how to point to the above.
char (*preset_configurations[3])[16] = {
    NULL, // this list is 1-based
    fruits,
    colors,
};

That way the strings are no longer constant strings (which, as you said, the exec functions don't want).

ooga
  • 15,423
  • 2
  • 20
  • 21
  • const preservation FTW! – ooga Jan 31 '14 at 23:31
  • Is it possible for me to have `preset_configurations` be a `char * const*`? I am trying to pass it to the linux `execv` function which requires `char * const*`. – boltup_im_coding Jan 31 '14 at 23:33
  • Just remove the first const from all of them. Although, technically, you should have `char const` since you're pointing to string constants. – ooga Jan 31 '14 at 23:34
  • @ooga note, that will flag at least a compiler warning for deprecation since the literal is const by nature. Most C-compilers will allow it regardless unless some pretty pedantic warnings are being used. Ex: clang will report: "Initializing `'char *const *'` with an expression of type `'const char *const [3]'` discards qualifiers in nested pointer types." – WhozCraig Jan 31 '14 at 23:37
  • Right, I should have said "use actual char arrays and initialize them with your strings". – ooga Jan 31 '14 at 23:40
  • Hmmm.. I think I got it. I had the const's that way because of my question here: http://stackoverflow.com/questions/21491713/array-element-is-a-variable-in-array-of-strings-in-c/21491928 – boltup_im_coding Jan 31 '14 at 23:40
  • Can you explain a little about what's going on? I am new to C and hate asking these dumb questions. I would like to understand better `const * const *` and arrays, etc... – boltup_im_coding Jan 31 '14 at 23:41
  • 1
    @unexpected62 ensconced within [this answer to a somewhat related question](http://stackoverflow.com/questions/14562845/why-does-passing-char-as-const-char-generate-a-warning/14566215#14566215) is a diatribe on every conceivable meaning of `const` and `*` through double-indirection (i.e. pointers to pointers). It may help you understand better. – WhozCraig Jan 31 '14 at 23:43
0
typedef const char const *(*cp3)[3];
cp3 preset_configurations[3] = {
    NULL,
    &fruits,
    &colors,
};
//printf("%s\n", (*preset_configurations[1])[2]);//orange
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70