Home > Mobile >  Assign values of one array of pointer to another array of pointer
Assign values of one array of pointer to another array of pointer

Time:08-13

string *ptr1[10];
string *ptr2[10];
ptr2 = ptr1; //doesn't work

I want ptr2 to have the values of ptr1. How can I achieve this without using loop?

CodePudding user response:

If you are using pointers to std::strings, then you can use a C function memcpy.

#include <cstring> // to use memcpy
string* str1[10];
string* str2[10];  
memcpy(str2, str1, 10*sizeof(str1[0])); // 10 * size of stored element (here string*)

However note, that using memcpy might be tricky (for example it wouldn't work if you used an array of std::string instead of an array of pointers to std::string). Loop would be the best approach.

  • Related