Solved

How to use pointer array data in MFC?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

911 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

16 Experts available now in Live!

Get 1:1 Help Now