Combine multiple char *

Ok. This should not be complicated: I have an array of char pointers and I would like to combine the array into one char pointer. So for kicks, lets say I have this char * array:

char *chars[16] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"};

How do I combine these into the equivalent of

char *equivalent = "abcdefghijklmnop";
char buffer[17];

for ( int8_t i = 0; i < 16; ++i )
{
  buffer[i] = *chars[i];
}
buffer[16] = 0;

You could use
String strings[]= { "abc", "d", "ef", "ghi" };
String s = strings[2];
etc

but there are caveats to using String, the most important of which I think is to make sure you're using a fixed "malloc" ( http://code.google.com/p/arduino/issues/detail?id=857#c8).

Cheers,
John

fuzzball27:
Ok. This should not be complicated: I have an array of char pointers and I would like to combine the array into one char pointer.

That's a curious thing to want to do. Just out of nosiness, why do you want to do that?

char *chars[16] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"};

The answer actually depends on what exactly you meant by chars[] declaration.

As is, it is declared as an array of char pointers (aka an array of strings), and initialized as such. Except that they are initialized to strings consisting of 1 char (plus 1 null).

So if you meant it to be an array of strings that happen to be initialized to 1 char, none of the solutions provided so far will work.

If you meant it to be an array of chars, like:

char chars[16] = {'a', 'b', ..., 'p'};

then there is nothing to be done: chars is the pointer to a string.

dhenry:
So if you meant it to be an array of strings that happen to be initialized to 1 char, none of the solutions provided so far will work.

That is incorrect. Coding Badly provided an answer that does exactly what was requested.

dhenry:
If you meant it to be an array of chars, like:

char chars[16] = {'a', 'b', ..., 'p'};

then there is nothing to be done: chars is the pointer to a string.

You have neglected to null-terminate the string in your example.

PeterH:
That's a curious thing to want to do. Just out of nosiness, why do you want to do that?

Well actually it's for a cocoa application to unlock a PDF, but I needed some low-level advice in a short amount of time, so I asked here :slight_smile:

I don't remember what I ended up doing (It's been a long weekend), but the program did what I needed and I'm done now haha

Thanks for all the help guys!