duplicate array

hello all, I'm trying to duplicate a string array.
i created a function called duplicate but, when i run it, it gives me an error... what is wrong with it?

thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


#include <iostream>
#include <string>
#include <fstream>
#include <array>
#define ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0])))


using namespace std;

void duplicate(string temp[], string temp2[], int lenght);

int main()
{
	
string words[5]={ "sebastian", "peter", "catalina", "angela", "martina", };
	string duplicateWords[5];
	
	
	int lenghtOfWords;
	lenghtOfWords = ARRAY_SIZE(words);
	

	duplicate(words, duplicateWords,lenghtOfWords);

	for (int i = 0; i < 15; ++i) // display contents of words
	{
		cout << words[i] << endl;
	}

	for (int i = 0; i < 15; ++i) // display contents of the duplicate
	{
		cout << duplicateWords[i] << endl;
	}


		system("pause");
	
}


void duplicate(string temp[], string temp2[], int lenght2){
	 
	for (int i = 0; lenght2; i++){
		temp[i] = temp2[i];
	}
	
}

On line 45, the condition lenght2 should be i < lenght2
and on the next line it looks like copy is taking place in the wrong direction (copying from the duplicate to the original).
thank you!! that took care of it!
would the items be pointing to the same address? or same value with different address?
The duplicate is completely independent of the original. First string duplicateWords[5] allocates an array of empty strings. Then the loop copies the contents of each string, so the values are the same, but the address where it is stored will be different.
Topic archived. No new replies allowed.