Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assign values of one array of pointer to another array of pointer

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading