-
April 4th, 2014, 12:49 AM
#1
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.
◄◄ hypheni ►►
-
April 4th, 2014, 01:08 AM
#2
Re: Array of CString size checking
Code:
#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
size_t iSiz = COUNTOF(m_cstrArray);
Best regards,
Igor
-
April 4th, 2014, 01:17 AM
#3
Re: Array of CString size checking
Originally Posted by hypheni
Code:
//Class header
CString m_cstrArry[5];
Is there some serious reason for you to NOT use CStringArray class?
Victor Nijegorodov
-
April 4th, 2014, 01:35 AM
#4
Re: Array of CString size checking
Yes, actually I'm doing my part of code on top of another person's implementation.
◄◄ hypheni ►►
-
April 4th, 2014, 01:37 AM
#5
Re: Array of CString size checking
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 ?
◄◄ hypheni ►►
-
April 4th, 2014, 02:34 AM
#6
Re: Array of CString size checking
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.
Victor Nijegorodov
-
April 4th, 2014, 03:30 AM
#7
Re: Array of CString size checking
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.
◄◄ hypheni ►►
-
April 4th, 2014, 06:14 AM
#8
Re: Array of CString size checking
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.
Best regards,
Igor
-
April 5th, 2014, 12:54 PM
#9
Re: Array of CString size checking
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|