Reputation: 199
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.
Upvotes: 0
Views: 1512
Reputation: 772
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]);
}
}
Upvotes: 1