C 连接两个字符串数组?
我是c世界的新手,我想把两个数组合并成一个数组,我有一个办法,但是不行:pC 连接两个字符串数组?,c,arrays,merge,memcpy,C,Arrays,Merge,Memcpy,我是c世界的新手,我想把两个数组合并成一个数组,我有一个办法,但是不行:p char *s_one[] = { "Zorro", "Alex", "Celine" }; char *s_two[] = { "Zorro1", "Alex1"}; char *p = (char*)malloc((sizeof(s_one)+sizeof(s_two))*sizeof(char)); memcpy(p, s_one, sizeof(s_one)); memcpy(p + sizeof(s_one)
char *s_one[] = { "Zorro", "Alex", "Celine" };
char *s_two[] = { "Zorro1", "Alex1"};
char *p = (char*)malloc((sizeof(s_one)+sizeof(s_two))*sizeof(char));
memcpy(p, s_one, sizeof(s_one));
memcpy(p + sizeof(s_one), s_two, sizeof(s_two));
//print out
for (count = 0; count < sizeof(p); count++)
printf("\narr[%d] = %c.", count, p[count]);
char*s_one[]={“佐罗”、“亚历克斯”、“塞琳”};
char*s_two[]={“Zorro1”,“Alex1”};
char*p=(char*)malloc((sizeof(suone)+sizeof(sutwo))*sizeof(char));
memcpy(p,s_one,sizeof(s_one));
memcpy(p+sizeof(s_-one),s_-two,sizeof(s_-two));
//打印出来
对于(count=0;count
输出只是一些随机字符。。。
我做错了什么,提前谢谢你的每一个提示
输出应为:
佐罗
亚历克斯
赛琳
佐罗1
Alex1我建议不要使用memcpy,而是迭代两个数组中的每一个,并使用strcpy将一个字符串从源数组复制到p中。memcpy的问题是它应该忽略复制\0字符,因此是随机字符
你的指令很好 让我们看看我能不能搞定
s\u one
是char*
的数组。因此,您的sizeof()
和memcpy()
操作可能是在指针上操作的,而不是它们指向内存中其他地方的字符串。然后,当您转到并printf()
时,您将指针打印为字符,而不是它们所指向的字符串(作为字符串)。这里不确定,但也许这样行吗
printf("\narr[%d] = %s.", count, *p[count]);
如果你在我回家之前不把它修好,我会试试看
结合Marvo关于
sizeof()
调用的评论
这真的取决于你想要什么,但这是我的尝试,因为我说过我会发布它:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *s_one[] = { "Zorro", "Alex", "Celine" };
char *s_two[] = { "Zorro1", "Alex1"};
printf("%lu\n", sizeof(s_one));
printf("%lu\n", sizeof(s_two));
int numberOfEntries = (sizeof(s_one) + sizeof(s_two)) / sizeof(char*);
char **p = (char **)malloc(numberOfEntries);
printf("%d\n", numberOfEntries);
memcpy(p, s_one, sizeof(s_one));
memcpy(p + sizeof(s_one)/sizeof(char *), s_two, sizeof(s_two));
//print out
int count = 0;
for (count = 0; count < numberOfEntries; count++)
printf("arr[%d] = %s.\n", count, p[count]);
}
#包括
#包括
#包括
内部主(空)
{
char*s_one[]={“佐罗”、“亚历克斯”、“塞琳”};
char*s_two[]={“Zorro1”,“Alex1”};
printf(“%lu\n”,sizeof(s_one));
printf(“%lu\n”,sizeof(s_two));
int numberOfEntries=(sizeof(s_one)+sizeof(s_two))/sizeof(char*);
char**p=(char**)malloc(numberOfEntries);
printf(“%d\n”,numberOfEntries);
memcpy(p,s_one,sizeof(s_one));
memcpy(p+sizeof(s_-one)/sizeof(char*)、s_-two、sizeof(s_-two));
//打印出来
整数计数=0;
for(count=0;count
您有两个指向字符的指针数组(实际上指向以0结尾的字符[]
)。因此,当您将memcpy
从su one
和su two
复制到p
时,您将复制指针,并且打印出的是指针值的一部分
如果你申报
char **p = malloc(sizeof s_one + sizeof s_two);
您将得到一个由五个字符组成的数组,指向相应的字符串
如果要连接s_one
resp的元素所对应的字符串s_two
指向,您需要分配足够的数据来保存结果(加上0-终止符):
size\u t needed=1;
对于(大小i=0;i
对于噪音较小的malloc,只需两个提示:
您不需要强制转换它的返回值。
您不需要sizeof(char),因为char的定义是1个字节。我的第一个解决方案是迭代两个数组中的每一个,并使用strcpy将一个字符串从源数组复制到p…感谢Hernan Velasquez
char *s_one[] = { "Zorro", "Alex", "Celine" };
char *s_two[] = { "Zorro1", "Alex1"};
char *p[sizeof(s_one)/sizeof(char *) + sizeof(s_two)/sizeof(char *)];
memset(p, 0, sizeof(p));
for(count=0;count < sizeof(s_one)/sizeof(char *) ;count++) {
p[count] = (char*)malloc((strlen(s_one[count])+1)*sizeof(char));
strcpy(p[count], s_one[count]);
}
i=count;
for(count=0;count < sizeof(s_two)/sizeof(char *) ;count++) {
p[i] = (char*)malloc((strlen(s_two[count])+1)*sizeof(char));
strcpy(p[i], s_two[count]);
i++;
}
//print out
for (count = 0; count < sizeof(p)/sizeof(char *); count++)
printf("\narr[%d] = %s.", count, p[count]);
char*s_one[]={“佐罗”、“亚历克斯”、“塞琳”};
char*s_two[]={“Zorro1”,“Alex1”};
char*p[sizeof(s_one)/sizeof(char*)+sizeof(s_two)/sizeof(char*)];
memset(p,0,sizeof(p));
对于(count=0;count
我认为它不是很干净,但它对我很有效。。。
明天我用Daniel Fischer的tipp来尝试它char*x[]实际上是一个指针数组调用应该是将两个数组的组合大小乘以一个char*的大小,而不是char。这可能无法解决问题,但很可能是个问题。您的输出规范清楚地表明您没有尝试合并这两个字符串数组。您正在尝试将它们连接起来。我喜欢您关于在ar上迭代的建议但是malloc指令实际上被破坏了,因为它乘以了sizeof(char),而不是sizeof(char*)。
char *s_one[] = { "Zorro", "Alex", "Celine" };
char *s_two[] = { "Zorro1", "Alex1"};
char *p[sizeof(s_one)/sizeof(char *) + sizeof(s_two)/sizeof(char *)];
memset(p, 0, sizeof(p));
for(count=0;count < sizeof(s_one)/sizeof(char *) ;count++) {
p[count] = (char*)malloc((strlen(s_one[count])+1)*sizeof(char));
strcpy(p[count], s_one[count]);
}
i=count;
for(count=0;count < sizeof(s_two)/sizeof(char *) ;count++) {
p[i] = (char*)malloc((strlen(s_two[count])+1)*sizeof(char));
strcpy(p[i], s_two[count]);
i++;
}
//print out
for (count = 0; count < sizeof(p)/sizeof(char *); count++)
printf("\narr[%d] = %s.", count, p[count]);