Pages: [1]   Go Down
Author Topic: Combine multiple char *  (Read 970 times)
0 Members and 1 Guest are viewing this topic.
United States
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Indubitably
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
char *equivalent = "abcdefghijklmnop";
Logged

fuzzball27 >>-->

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13039
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char buffer[17];

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

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
char chars[16] = {'a', 'b', ..., 'p'};

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: January 05, 2013, 12:27:17 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

United States
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Indubitably
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

fuzzball27 >>-->

Pages: [1]   Go Up
Jump to: