连接数组的元素 [英] concatenating elements of an array
问题描述
大家好,
我一直试图让这个工作好几天,但我仍然无法得到它。
片段:
unsigned char * a;
unsigned msg [5];
我想连接数组msg中的所有5个元素unsigned char * a。
让我们说
msg [0] =" abc";
msg [1] =" def" ;;
msg [2] =" ghi";
msg [3] =" jkl";
msg [4] =" mno";
,最后a指向一个值=" abcdefghijklmno"。
我已经尝试了常规但是仍然不成功。如果有人能够启发我,请欣赏它。谢谢。
for(int i = 0; int< 5; i ++)
a = a + msg [i]
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]
推荐答案
查看 strcat()功能。
亲切的问候,
Jos
Have a look at the strcat() function.
kind regards,
Jos
别忘了为最终字符串分配足够大的char缓冲区。您需要在缓冲区中组合串联并将a设置为指向该缓冲区。
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.
man ...分段错误...我无法弄明白。所以我只是用一个捷径。将所有这些数组写入并附加到文件中,然后将其作为字符串读回。谢谢所有
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
这篇关于连接数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!