[英]Copying array of char pointers
我想知道如何将以下容器复制到临时变量,以及该临时变量应如何定义。
const char *containers_1[] = {"one","two","why do I stuck in this problem"};
const char *containers_2[] = {"Other","string","here"};
因此,我正在寻找一个合适类型的临时变量,可以将这些容器之一复制到其中。 "const char * container []"
的声明来自一段我不想更改以保持其格式美观的代码!
谢谢你的时间。
代码应该改进,但是我认为这是您想要的。
const char *containers_1[] = {"one","two","why do I stuck in this problem"};
const char *containers_2[] = {"Other","string","here","whis","has","more"};
main(int argc, char **argv) {
char ** tmp1;
int i, size;
size = sizeof(containers_1);
printf ("%d\n", size);
tmp1 = malloc(size);
memcpy(tmp1, containers_1, sizeof(containers_1));
for (i=0; i< size/sizeof(char *); i++) {
printf("%s\n", tmp1[i]);
}
size = sizeof(containers_2);
printf ("%d\n", size);
tmp1 = malloc(size);
memcpy(tmp1, containers_2, sizeof(containers_2));
for (i=0; i< size/sizeof(char *); i++) {
printf("%s\n", tmp1[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.