让我们看看我是否能做到这一点......
s_one
是一个数组char *
。所以,你的sizeof()
and memcpy()
操作可能是对指针进行操作,而不是对它们指向内存中其他位置的字符串进行操作。然后,当你去的时候printf()
您将指针打印为字符而不是它们指向的字符串(作为字符串)。不确定这里,但也许这会起作用?
printf("\narr[%d] = %s.", count, *p[count]);
如果我回家之前你没修好,我会测试一下看看。
结合 Marvo 的评论sizeof()
calls.
这实际上取决于你想要什么,但这是我的尝试,因为我说过我会发布它:
#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]);
}