copying values from an array to an array in a function.

Is it possible for me to copy an array of string values which is declared in my main method to a function which is located in a class file?


Sure. Are you using char* or std::string?
i`m using std::string


for more info here are my code snippets

1
2
 dataComb=getData.toString()+data.toString();// i combined the values of string of x,y cordinates and sunType etc
 dataStore[counter]=dataComb;// i stored them here 


as you can see i am trying to copy the values of dataStore[] into my function computeCivicIndex()

which is

1
2
3
4
5
6
7
8
float locationData::computeCivicIndex(string dataStore [])
{
	int counter=0;
    for(int i=0;i<=counter;i++);
    dataStore[i]=dataStore[i];


}


i tried doing it in this way but it returns me with a error which says
locationData.cpp: In member function ‘float locationData::computeCivicIndex(std::string*)’:
locationData.cpp:101: error: name lookup of ‘i’ changed for ISO ‘for’ scoping
locationData.cpp:101: note: (if you use ‘-fpermissive’ G++ will allow the code to compile)

may i know the correct way of doing this?
i am supposed to take in the values from the array and used it in the computation process in computeCivicIndex() method
That error is due to the the ; on line 4. Although assigning thing to itself is no good anyway. To make a copy of array arr of type T and length len do
1
2
T* copy = new T[len];
for(int i = 0; i < len; i++) copy[i] = arr[i];

Don't forget to delete copy later.
Although if you use std::vector instead of an array, the copy will be made automatically when passed by value or on plain assignment. If you ever want to avoid copy, pass by reference.
Topic archived. No new replies allowed.