469,304 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

concatenating elements of an array

15
Hi all,
I've been trying to get this working for days but I still couldn't get it.

snippet:
unsigned char *a;
unsigned msg[5];

I want to concatenate all 5 elements from array msg to the unsigned char *a.
Lets say
msg[0]="abc";
msg[1]="def";
msg[2]="ghi";
msg[3]="jkl";
msg[4]="mno";

and in the end a would point to a value = "abcdefghijklmno".
I've tried the usuals but still unsuccessful. appreciate it if anyone could enlighten me on this. thanks.
for(int i=0;int<5;i++)
a = a + msg[i]
Dec 26 '08 #1
14 14656
JosAH
11,448 Expert 8TB
Have a look at the strcat() function.

kind regards,

Jos
Dec 26 '08 #2
donbock
2,422 Expert 2GB
Don't forget to allocate a big enough char buffer for the final string. You need to assemble the concatenation in a buffer and set a to point to that buffer.
Dec 27 '08 #3
boddah
15
man...segmentation error..i cant figure it out. so i just use a short-cut. Write and append all those arrays to a file and then read 'em back as a string. thanks all
Dec 31 '08 #4
JosAH
11,448 Expert 8TB
@boddah
That's like going from Dallas to Fort Worth over New York; I don't call that a short cut ;-) First calculate the sum of the lengths of the strings plus one for the terminating \0 character. Find (malloc) yourself a char buffer of at least that size and copy those strings in, one after another.

Expand|Select|Wrap|Line Numbers
  1. int calc_length(char** msg, int msglen) {
  2.    int sum;
  3.    for (sum= 0; msglen--; sum+= strlen(*msg++));
  4.    return sum;
  5. }
  6. char* catenate(char** msg, int msglen, char* dst) {
  7.    char* p;
  8.    for (p= dst; msglen--; strcpy(p, *msg++), p+= strlen(p));
  9.    return dst;
  10. }
  11.  
kind regards,

Jos
Dec 31 '08 #5
boddah
15
@JosAH
Thanks Jos...I've been using malloc and new but i always get a segmentation fault. I'm quite new to all this malloc and sizeof.
Dec 31 '08 #6
JosAH
11,448 Expert 8TB
@boddah
You either have to use malloc/free or new/delete, never use a combination thereof. Show us some of your latest code so we can see what's wrong ...
(not the file stuff, show your version before that).

btw, if you use my version and malloc/new as many bytes as the number returned by the first function you can safely call the second function that does the catenating job.

kind regards,

Jos
Dec 31 '08 #7
boddah
15
man..i still can't solve this.

unsigned char array[20];
char cr[21];
strncpy(cr, (char*)array,20);
printf("aha:%s\n",pcr);

-it still gives me segmentation fault. can someone pls help?
Jan 14 '09 #8
vekipeki
229 Expert 100+
What is pcr if I may ask?
Check the last line, where you used "pcr" instead of "cr":

Expand|Select|Wrap|Line Numbers
  1. unsigned char a[20] = "test"; 
  2. char cr[21];
  3. strncpy(cr, (char*)a, 20);
  4. printf("aha:%s\n", cr);
  5.  
Jan 14 '09 #9
boddah
15
ah..yes, no prob. it's supposed to be cr. my typo
@vekipeki
Jan 14 '09 #10
donbock
2,422 Expert 2GB
@boddah
You shouldn't cast casually. Sometimes a cast is necessary, but often it is a sign that you're doing something you shouldn't.

This thread has gotten pretty long. I'm making a conscious decision to stop being coy. Please look at the following example code. Ask about the rationale for any differences from your code that you don't understand. There is plenty of room for further improvement.

Expand|Select|Wrap|Line Numbers
  1. /* Notice use of "char", not "unsigned char" */
  2. char *a;
  3. char *msg[5] = {
  4.      "abc", "def", "ghi". "jkl", "mno" };
  5. const int nmsgs = 5;
  6. size_t len;
  7. int i;
  8.  
  9. /* compute length of concatenated string (plus 1 for terminating null */
  10. len = 0;
  11. for (i=0; i<nmsgs; i++)
  12.    len += strlen(msg[i]);
  13. /* allocate output buffer */
  14. a = malloc(len+1);
  15. if (a == NULL) {
  16.    handle the out-of-memory error
  17. }
  18.  
  19. /* Initialize output buffer to 'empty' */
  20. a[0] = '\0';
  21. /* concatenate each msg entry in turn */
  22. for (i=0; i<nmsgs; i++)
  23.    strcat(a,msg[i]);
  24. /* print the result */
  25. printf("%s\n", a);
  26. ...
  27. /* Don't use pointer after the buffer is freed */
  28. free(a);
Jan 14 '09 #11
Tassos Souris
152 100+
Just a little note for the above code:
Expand|Select|Wrap|Line Numbers
  1. malloc( ... * sizeof( char ) );
  2.  
Well, it is a good habit to use that
Jan 14 '09 #12
JosAH
11,448 Expert 8TB
@Tassos Souris
I'm in the opposite camp: sizeof(char) equals one by definition so there is no need to multiply by that number.

kind regards,

Jos
Jan 14 '09 #13
Tassos Souris
152 100+
You are right Jos but i said: Well, it is a good habit to use that
With that i meant that it is not a must but something good to use cause (at least for me) it keeps remind me that when using malloc,calloc,realloc (or another memory allocation function from other libraries) i need that sizeof() thingy...
As i said just a habit i use! My bad not to explain that more thoroughly!
Jan 14 '09 #14
JosAH
11,448 Expert 8TB
@Tassos Souris
Easy; no need to get upset about it ;-) feel free to multiply a number by one if you want; be my guest. The optimizer most likely removes that multiplication anyway. I'm just in the 'no news is good news' camp and C and verbosity don't mix well (by tradition).

kind regards,

Jos
Jan 14 '09 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Unforgiven | last post: by
4 posts views Thread by Richard L Rosenheim | last post: by
21 posts views Thread by c | last post: by
17 posts views Thread by eBob.com | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.