Go Down

Topic: Combine multiple char * (Read 1 time) previous topic - next topic

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. So for kicks, lets say I have this char * array:
Code: [Select]
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
Code: [Select]
char *equivalent = "abcdefghijklmnop";
fuzzball27 >>-->

Coding Badly

Code: [Select]

char buffer[17];

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

johncc

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

PeterH


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?
I only provide help via the forum - please do not contact me for private consultancy.

dhenry

Code: [Select]
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:

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

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

PeterH

#5
Jan 05, 2013, 06:25 pm Last Edit: Jan 05, 2013, 06:27 pm by PeterH Reason: 1

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.


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

Code: [Select]
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.
I only provide help via the forum - please do not contact me for private consultancy.

fuzzball27


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

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!
fuzzball27 >>-->

Go Up