简体   繁体   English

复制char指针数组

[英]Copying array of char pointers

I would like to know how can I copy this following container to a temporary variable and how should that temp variable be defined as well. 我想知道如何将以下容器复制到临时变量,以及该临时变量应如何定义。

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

So I am looking for a temp variable of a suitable type which I can copy one of those containers to. 因此,我正在寻找一个合适类型的临时变量,可以将这些容器之一复制到其中。 The declaration of "const char * container []" is from a piece of code that I don't wanna change to keep the format nicely! "const char * container []"的声明来自一段我不想更改以保持其格式美观的代码!

Thanks for your time. 谢谢你的时间。

The code should be improved however I think this is what you want. 代码应该改进,但是我认为这是您想要的。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM