dynamic array of strings

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.

1
2
3
4
5
6
7
8
9
10
11
string *stringTerms;
process(stringTerms);
cout << stringTerms;

process(string *stringTerms)
{
int numWords = 10;
stringTerms = new string[numWords];
cout << stringTerms;
return;
}
This is one method - using reference to pointer
( You really need to learn pointers and references - very important part of C++)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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()
Hi guestgulkan,

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??
You can recheck what I posted
Anyone out there know how to determine the number of elements in an array of strings??


If the array was dynamically allocated, there is no way.

You have to keep track of it in some other variable.


@guestgulkan: ew @ passing ownership.
Thanks Disch,

I can do that, I just wasn't sure if I could do that or not..

Thanks to you all! I got this!
Topic archived. No new replies allowed.