Language: Visual C++, MFC
Environment: Visual Studio 2005
So I posted a similar question, but I’ve come to realize that I was asking the wrong question. I’m trying to use a loop to call a function on several different variables, but somewhere along the way, the program is crashing.
Simplified code is below, but I think it’s actually easier to just explain it. I have a function that takes in a CString as a parameter. I have several variables I wish to feed to this function, so I put their names into an array, and I’m trying to pass them to the function that way.
// THE CODE BELOW IS WHAT I HAVE, BUT IT DOES NOT WORK //
Header File:
CString m_strTop;
CString m_strLeft;
CString m_strRight;
CString m_strBottom;
CString *var[4];
Source File:
Constructor()
CString *var[4] = {
&m_strTop
, &m_strLeft
, &m_strRight
, &m_strBottom
};
Source File:
theFunction()
void myClass::DoDataExchange(CDataExchange* pDX)
{
CSAPrefsSubDlg::DoDataExchange(pDX);
for(int i = 2001, j = 0; i <= 2004; i++, j++)
{
// THE LINE BELOW IS WHERE THINGS GO WONKY, SPECIFICALLY AT &var[j]
DDX_Text(pDX, i, *var[j]); // 'i' is the ID of the textbox
}
}
— What DDX_Text expects —
void AFXAPI DDX_Text(
CDataExchange* pDX,
int nIDC,
CString& value
);
So like I said, I just need to feed the function the actual name of the variable. At least I think. What it’s actually doing is establishing a connection between a text box in a dialog and the variable where the text box’s input will be stored. I’m dereferencing correctly and everything, but I don’t think this is the right approach.
Thank you for any help. And to people who answered my previous question, my apologies for misrepresenting the issue.
varis an array of pointers toCString.var[j]is a pointer toCString.&var[j]is a pointer to pointer toCString.Now you need to pass the
CStringobject. So you need:Consider using
std::vectorinstead of the C-array. It would be:I’ve noted that you’re declaring variable
varonce again in the constructor:Shouldn’t it be? :
I suppose you need the following: