Solved

How to use pointer array data in MFC?

Posted on 2008-10-07
6
525 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 250 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 250 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now