CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    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 ►►

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Array of CString size checking

    Code:
    #define COUNTOF(x)  (sizeof(x)/sizeof(x[0]))
    
    size_t iSiz = COUNTOF(m_cstrArray);
    Best regards,
    Igor

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Array of CString size checking

    Quote Originally Posted by hypheni View Post
    Code:
      //Class header
    
      CString m_cstrArry[5];
    Is there some serious reason for you to NOT use CStringArray class?
    Victor Nijegorodov

  4. #4
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Array of CString size checking

    Yes, actually I'm doing my part of code on top of another person's implementation.
    ◄◄ hypheni ►►

  5. #5
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Array of CString size checking

    Quote Originally Posted by Igor Vartanov View Post
    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 ►►

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Array of CString size checking

    Quote Originally Posted by hypheni View Post
    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

  7. #7
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Array of CString size checking

    Quote Originally Posted by VictorN View Post
    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 ►►

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Array of CString size checking

    Quote Originally Posted by hypheni View Post
    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

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Array of CString size checking

    Quote Originally Posted by Igor Vartanov View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured