Solved

How to use pointer array data in MFC?

Posted on 2008-10-07
6
542 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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. …
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: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

729 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