简体   繁体   中英

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!

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]);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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