Well, here's one way you could go about it. I've passed the vector by reference (put the & char in front of the var name in the function sig) because we may be talking about megabytes of data. There's no need to copy and pass all of that data to the function - we can simply tell the function where to find it.
I could have done this with pointers * or references &
It's much of a muchness in this situation.
Though do be aware - by passing as a pointer or a reference, if you modify the input argument in the function, it will modify the actual data itself - not just a copy of it.
You can try this by clearing the values of each struct in method 1, after you've printed them. Notice now how method 2 shows the new values? If you then remove the & from the function signature and try again, notice there's no effect?
Also, shouldn't really be mixing C/C++ code - i.e FUN.c should probably be a string.
As it stands, I don't de-allocate the memory gained with the calls to strdup - this will create a memory leak.. But that's another matter. I've aimed to keep the code understandable for you. :)
Anyway, enough ado about nothing. Here's the code:
#include <iostream>
#include <vector>
#include "string.h"
using namespace std;
struct Fun
{
int a;
char* c;
}FUN;
typedef vector<Fun> vecFun;
typedef vecFun::iterator vecFunIter;
void method1(vecFun inputList)
{
cout << "-------------------------------------------------------" << endl;
cout << "method 1 for access - use the [] operator of the vector" << endl;
cout << "-------------------------------------------------------" << endl;
int i, n;
n = inputList.size();
for (i=0; i<n; i++)
{
cout << "Activity: " << inputList[i].c << ", Fun level: " << inputList[i].a << endl;
inputList[i].a = 100;
}
}
void method2(vecFun &inputList)
{
cout << "-------------------------------------" << endl;
cout << "method 2 for access - use an iterator"<<endl;
cout << "-------------------------------------" << endl;
vecFunIter myIter;
for (myIter=inputList.begin(); myIter!=inputList.end(); myIter++)
{
cout << "Activity: " << myIter->c << ", Fun level: " << myIter->a << endl;
}
}
int main()
{
Fun myTemp;
vecFun funList;
myTemp.a = -1;
myTemp.c = strdup("Having teeth pulled");
funList.push_back(myTemp);
myTemp.a = 1;
myTemp.c = strdup("Having haircut");
funList.push_back(myTemp);
myTemp.a = 4;
myTemp.c = strdup("Coding");
funList.push_back(myTemp);
method1(funList);
cout << endl;
method2(funList);
vecFunIter mIter;
for (mIter=funList.begin(); mIter!=funList.end(); mIter++)
free(mIter->c);
}