Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use pointer array data in MFC?

Posted on 2008-10-07
6
Medium Priority
?
549 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

719 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