簡體   English   中英

復制char指針數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM