6

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?)
{
}
unwind
  • 391,730
  • 64
  • 469
  • 606
Sunscreen
  • 3,452
  • 8
  • 34
  • 40

6 Answers6

4

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 );
  }
}
ur.
  • 2,890
  • 1
  • 18
  • 21
4

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:

  • be aware that all the memory for nMySize default-constructed CStrings will be allocated by that single new statement
  • you should consider using a std::vector<std::string>
    • std::vector because:
      • automatically deletes the memory for all the strings when it goes out of scope
      • by default the memory usage will increase more gradually as strings are added using e.g. push_back(), and in such usage can grow beyond the initial size without any special work on your part
      • you can proactively reserve space for nMySize strings, and/or create them, when you construct the vector if you really want that behaviour
    • std::string because:
      • it's the portable string type defined by the C++ Standard, and reduces lock in to your dependencies
      • that said, it may be impractical or inefficient to avoid in some circumstances
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
3

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");
        }
    }
}
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • +1 for nice "C++-y" iterator style - while of no practical benefit here, it discourages the presumption that the function is necessary filling the entire array. Templates are a nice idea, though if the data type being assigned into the elements is too specific, they become less useful without further flexibility for the generator. "this would save you having to worry about how big the array is in your function" - not true as passing `&array[n]` versus `n` clearly doesn't save much ;-P. Not having to pass either would be a save. – Tony Delroy Nov 26 '10 at 09:33
  • Thanks Tony. Good point about `&array[n]`, what I meant was this removes the size from the implementation of the function. Iterators have many other benefits that I didn't go into (consider reversing the order, selective predicates, etc...). – johnsyweb Nov 26 '10 at 09:37
1

You need to pass the pointer to the first element, and the number of available elements:

void FillThisArray(CString* strings, size_t num)
{
}
unwind
  • 391,730
  • 64
  • 469
  • 606
0

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

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
-1
void FillThisArray(CString* pstrArray)

Can't u do this?

wliao
  • 1,416
  • 11
  • 18