Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to use pointer array data in MFC?

Posted on 2008-10-07
6
Medium Priority
?
551 Views
Last Modified: 2013-12-14
Dear Experts,

I am passing the pointer array to my function by using MFC exe in visual c++. The followings are my details.

//////////////////////////////////////////////////////////////////////////////////////////////////
In MyAppDlg.h
protected:
      HICON m_hIcon;

      // Generated message map functions
      //{{AFX_MSG(CMy7_10_V2Dlg)
      ...
                ...
      afx_msg void check(CString* a);
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};

In MyAppDlg.cpp
void CMy7_10_V2Dlg::OnButton1()
{
      typedef unsigned char Byte;
      // TODO: Add your control notification handler code here
      MyString="aaa";
      check(&MyString);
}


In MyAppDlg.cpp

void CMy7_10_V2Dlg::check(CString* a)
{
                char *temp = new char[16];
      m_result = new char[16];
      for (int i=0;i<16;i++)
      {
            temp[i]= a[i];
            
      }
}
///////////////////////////////////////////////////////////////////////////////////////

My error is  "error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CString' (or there is no acceptable conversion)
Error executing cl.exe."
////////////////////////////////////////////////////////////////////////
Please kindly answer for me how to use the data which is inside the pointer array passed from another function.
Thanks in advance.
Regards.

      
0
Comment
Question by:inteq
  • 3
6 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 total points
ID: 22666082
I would use something like:

void CMy7_10_V2Dlg::check(CString* a)
{
     char *pa = a->GetBuffer();

      char *temp = new char[16];
      m_result = new char[16];
      for (int i=0;i<16;i++)
      {
            temp[i]= pa[i];
      }
      a->ReleaseBuffer();
}

also, ensure your string terminates with a null character
0
 
LVL 19

Expert Comment

by:alb66
ID: 22666633
Since you haven't to modify the string you can ass a const reference

// In MyAppDlg.cpp
 
void CMy7_10_V2Dlg::OnButton1()
{
      typedef unsigned char Byte;
      // TODO: Add your control notification handler code here
      MyString="aaa";
      check(MyString);
}
 
 
// In MyAppDlg.cpp
 
void CMy7_10_V2Dlg::check(const CString& a)
{
      char *temp = new char[16];
      m_result = new char[16];
      for (int i=0;i<16;i++)
      {
            temp[i]= a[i];
      }
}

Open in new window

0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 1000 total points
ID: 22666646
If you want to use a pointer you can use GetAt():
http://msdn.microsoft.com/en-us/library/aa314338(VS.60).aspx


void CMy7_10_V2Dlg::check(CString* a)
{
      char *temp = new char[16];
      m_result = new char[16];
      for (int i=0;i<16;i++)
      {
            temp[i]= a->GetAt(i);
      }
}

Open in new window

0
 
LVL 19

Expert Comment

by:alb66
ID: 22666669
Also:
temp[i]= (*a)[i];

Note that you have a problem with the string length. You chack 16 character but the string length is 3.

void CMy7_10_V2Dlg::check(const CString& a)
{
      char *temp = new char[16];
      m_result = new char[16];
 
      int nMax = min( 16, a.GetLength() );
 
      for (int i=0;i<nMax;i++)
      {
            temp[i]= a[i];
      }
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question