0

我想知道如何将以下容器复制到临时变量以及如何定义该临时变量。

const char *containers_1[] = {"one","two","why do I stuck in this problem"};
const char *containers_2[] = {"Other","string","here"};

所以我正在寻找一个合适类型的临时变量,我可以将其中一个容器复制到其中。的声明"const char * container []"来自一段我不想更改以保持格式良好的代码!

谢谢你的时间。

4

1 回答 1

1

代码应该改进,但我认为这是你想要的。

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]);
   }
}
于 2013-09-25T14:55:34.913 回答