error C2110: cannot add two pointers and application path
Hi,
I'm trying to teach my self visual c++ 6. I'm having trouble combining two variables that I'm using. This is an MFC application.
void CTestDlg::OnButton1()
{
char temp[30];
GetCurrentDirectory(30,temp);
char* c = "\\path_to_file";
MessageBox(temp + c);
}
thanks for any help.
[362 byte] By [
psmith_8] at [2007-11-19 16:00:55]

# 1 Re: error C2110: cannot add two pointers and application path
Hello,
You can't add two character pointers like that to get a concatenated string. Use the function strcat instead to add two strings.
Regards.
Pravin.
# 2 Re: error C2110: cannot add two pointers and application path
Hi,
I'm trying to teach my self visual c++ 6. I'm having trouble combining two variables that I'm using. This is an MFC application.
void CTestDlg::OnButton1()
{
char temp[30];
GetCurrentDirectory(30,temp);
char* c = "\\path_to_file";
MessageBox(temp + c);
}BTW, why do you think the path/name of the "current directory" will aways be not longer than 29? :confused: :wave:
It should be at least of MAX_PATH length!
I'd rewrite your code as:void CTestDlg::OnButton1()
{
TCHAR temp[MAX_PATH] = {_T("\0")};
if(GetCurrentDirectory(MAX_PATH, temp) == 0)
{
// error!
DWORD dwErr = ::GetLastError();
// display an error message
}
else
{
CString strFilePath (temp);
strFilePath += _T("\\path_to_file");
AfxMessageBox(strFilePath);
}
}
# 3 Re: error C2110: cannot add two pointers and application path
Beside that Victor and Pravin already said, just to clarify a little:
Taking a look at the title, I can presume that you want to append something to the "application path".
Note that not always the application directory (where the application resides) is the same as the current directory (which is associated to the process and is used by operating system to find/put "stuff" by default).
The application can be started using a different current directory and the current directory can be even changed at runtime for example by open file dialogs.
So, If I'm not wrong in my presumption, thake a look at this FAQ (http://www.dev-archive.com/forum/showthread.php?t=312471) too see how to get the application directory.
PS. Already said, but I have to point that you cannot add two pointers, no matter which type they are.
# 4 Re: error C2110: cannot add two pointers and application path
And if you wonder why you can't add two pointers, remember that a pointer is a memory address. So what would you expect to get when you add two memory addresses? That's why you can't add two pointers.
cilu at 2007-11-10 23:57:46 >

# 5 Re: error C2110: cannot add two pointers and application path
Thanks for replying so fast.
The program will be used as a form of an application index on a CD. so it will be in the root directory of the cd. I will be using the CD to store the applications that get downloaded off the net, and I don't want to have to manually go through the CD looking for the programs that are on it.
I will be removing the message box and replacing it with the WinExec function to execute the appropriate application on the CD. The application files are in a subdirectory, hence the reason as to why I was trying to add the two variables.
Peter
# 6 Re: error C2110: cannot add two pointers and application path
It would be mistake to use WinExec in Win32 system!
From MSDN:WinExec
The WinExec function runs the specified application.
Note This function is provided only for compatibility with 16-bit Windows. Win32-based applications should use the CreateProcess function.
.........
# 7 Re: error C2110: cannot add two pointers and application path
Thanks for replying so fast.
The program will be used as a form of an application index on a CD. so it will be in the root directory of the cd. I will be using the CD to store the applications that get downloaded off the net, and I don't want to have to manually go through the CD looking for the programs that are on it.
I will be removing the message box and replacing it with the WinExec function to execute the appropriate application on the CD. The application files are in a subdirectory, hence the reason as to why I was trying to add the two variables.
Peter
Still it's not guarantee that GetCurrentDirectory will always return the correct path of your application.
BTW. WinExec is an obsolete function. Call CreateProcess, _spawn, or ShellExecute instead.