I just have a couple of questions on how to use dynamic arrays. First let me say I don't know how to use pointers or arrays, etc.. but below is the abbreviated version of my code, so.. I created a pointer to strings called stringTerms, then pass it to the function process to create a new stringTerms with the size.. then I cout the stringTerms memory address, then I return to the main program, and cout the other stringTerms there..
The memory addresses are different, and its kind of what I expected, but so I guess this leads me to my 1st question.
1.) how can I have these 2 stringTerms to coincide and be the same?
2.) how can I get the size of the array for stringTerms? I've tried:
a.) sizeof(stringTerms)/sizeof(string *)
b.) sizeof(stringTerms)
c.) sizeof(*stringTerms)
Conclusion: I dont know what I'm doing here.. please help point me in the right direction, if you know what I'm talking about.
int process(string * &stringTerms)
{
//calculate number of strings required
//for this example we will keep it simp[le
int numWords = 10;
stringTerms = new string[numWords];
cout << stringTerms;//testing
return numWords;
}
int main()
{
string *stringTerms;
int numWords;
numWords= process(stringTerms);
cout << " " << stringTerms; //testing
//once completed
delete []stringTerms;
}
1.) do you mean the stringTerms in main and stringTerms in process()
i believe if you change process(string *stringTerms) to process(string& stringTerms) they will be the same.
2.) sizeof is for returning the size of a data type i think but i could be wrong. maybe since it is a string you could use stringTerms.size()
you are very right in that pointers and references are very important in c++, I been doing c++ for a year now, and just now starting to get it little by little.. so thank you for your help on how to implement that.
And kyle11778, thanks for your response as well.. stringTerms.size() did not work for me.
it is a pointer to an array of strings.. so, i dont know how that changes being able to determine its size..?
Anyone out there know how to determine the number of elements in an array of strings??