Reputation: 3564
I have created an array:
CString* pstrArray = new CString[nMySize];
Now how can I pass it to a function to be filled up? What is the actual parameter?
void FillThisArray(what goes here?)
{
}
Upvotes: 6
Views: 1745
Reputation: 141800
If there's a very good reason why you cannot use a standard container class, consider taking an iterator-style approach. This would save you having to worry about how big the array is in your function:
void FillThisArray(CString* begin, CString* end)
{
for (CString* iter = begin; iter != end; ++iter)
{
*iter = "Some text";
}
}
int main()
{
CString* pstrArray = new CString[nMySize];
FillThisArray(&pstrArray[0], &pstrArray[nMySize]);
for (int i = 0; i < nMySize; ++i)
{
assert(pstrArray[i] == "Some_text");
}
delete[] pstrArray;
}
You could even template your function so that it is not tied to the (questionable) implementation of pstrArray:
template <typename T>
void FillThisArray(T begin, T end)
{
for (T iter = begin; iter != end; ++iter)
{
*iter = "Some text";
}
}
int main()
{
{
CString* pstrArray = new CString[nMySize];
FillThisArray(&pstrArray[0], &pstrArray[nMySize]);
for (int i = 0; i < nMySize; ++i)
{
assert(pstrArray[i] == "Some text");
}
delete[] pstrArray;
}
{
std::vector<std::string> better(nMySize);
FillThisArray(better.begin(), better.end());
for (int i = 0; i < nMySize; ++i)
{
assert(better[i] == "Some text");
}
}
}
Upvotes: 3
Reputation: 106096
CString* pstrArray = NULL; pstrArray = new CString[nMySize];
For simplicity:
CString* pstrArray = new CString[nMySize];
Now how can i pass it to a function to be filled up? What is the actual parameter?
void FillThisArray(????) { }
The most obvious interface is:
void FillThisArray(CString* pstrArray, size_t n)
Taking a step back:
CString
s will be allocated by that single new statementstd::vector<std::string>
std::vector
because:
push_back()
, and in such usage can grow beyond the initial size without any special work on your partvector
if you really want that behaviourstd::string
because:
Upvotes: 4
Reputation: 2947
You should use a container class: CStringArray
void FillThisArray( CStringArray & rMyStrings )
If you don't want this (I don't see any possible reason, but anyway):
void FillThisArray(CString* strings, size_t num)
{
// example
for( size_t s=0; s<num; s++ )
{
strings[s].Format( _T("This is the %d. string"), s+1 );
}
}
Upvotes: 4
Reputation: 132994
You must pass a dymanically allocated array as a pointer to its first element. There are two syntaxes for that, both are equivalent:
void FillThisArray(CString* strArray)
{
}
or
void FillThisArray(CString strArray[])
{
}
you can use the strArray parameter as an array inside the function. Please note that the pointer doesn't hold information about your array's actual size, so if the size is not globally available you should pass the size as the second parameter. hth
Upvotes: 0
Reputation: 399823
You need to pass the pointer to the first element, and the number of available elements:
void FillThisArray(CString* strings, size_t num)
{
}
Upvotes: 1