Sunscreen
Sunscreen

Reputation: 3564

Array as a parameter

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

Answers (6)

johnsyweb
johnsyweb

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

Tony Delroy
Tony Delroy

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:

  • 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

Upvotes: 4

ur.
ur.

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

Armen Tsirunyan
Armen Tsirunyan

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

unwind
unwind

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

wliao
wliao

Reputation: 1416

void FillThisArray(CString* pstrArray)

Can't u do this?

Upvotes: -1

Related Questions