Array of CString size checking
Code:
//Class header
CString m_cstrArry[5];
Code:
//Class source
void Ctry4Dlg::OnInitDialog()
{
m_cstrArry[0] = _T("TEXT 0 |");
m_cstrArry[1] = _T("TEXT 1 |");
m_cstrArry[2] = _T("TEXT 2 |");
m_cstrArry[4] = _T("TEXT 4 |");
}
void Ctry4Dlg::CheckCStringRef(CString *cstrTextArr)
{
size_t iSiz = sizeof(cstrTextArr); //is it correct to calculate array length this way ?
CString cstrWholeText = _T("");
if(cstrTextArr != NULL)
{
for(int i=0; i<5; i++) //here I want to use returned array size ie: iSiz
{
cstrWholeText = cstrWholeText + cstrTextArr[i];
}
}
}
void Ctry4Dlg::OnBnClickedOk()
{
CheckCStringRef(m_cstrArry); //passing the array to my function to process further
}
Hope I'm clear about my qus by the commented lines.
Re: Array of CString size checking
Code:
#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
size_t iSiz = COUNTOF(m_cstrArray);
Re: Array of CString size checking
Quote:
Originally Posted by
hypheni
Code:
//Class header
CString m_cstrArry[5];
Is there some serious reason for you to NOT use CStringArray class?
Re: Array of CString size checking
Yes, actually I'm doing my part of code on top of another person's implementation.
Re: Array of CString size checking
Quote:
Originally Posted by
Igor Vartanov
Code:
#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
size_t iSiz = COUNTOF(m_cstrArray);
I need to check the size of the pointer when it has been captured inside my very own function. Can I use the same for CString *cstrTextArr ?
Re: Array of CString size checking
Quote:
Originally Posted by
hypheni
I need to check the size of the pointer when it has been captured inside my very own function.
Then you would probably need to pass in the siye of the array as additional parameter.
You could avoid it using the CStringArray or std::vector of CString elements.
Re: Array of CString size checking
Quote:
Originally Posted by
VictorN
Then you would probably need to pass in the siye of the array as additional parameter.
You could avoid it using the CStringArray or std::vector of CString elements.
Yes. Currently I'm passing the array size as int.
Re: Array of CString size checking
Quote:
Originally Posted by
hypheni
I need to check the size of the pointer when it has been captured inside my very own function. Can I use the same for CString *cstrTextArr ?
Size of a pointer is a constant value in particular OS. Besides, pointer and array are different data types.
Re: Array of CString size checking
Quote:
Originally Posted by
Igor Vartanov
Code:
#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
size_t iSiz = COUNTOF(m_cstrArray);
In C++ I would recommend using this instead:
Code:
template<typename T, size_t N>
size_t arraySize(T(&)[N])
{
return N;
}
It's much harder to use incorrectly.