zzZOsiroZzz
zzZOsiroZzz

Reputation: 23

error C2664: cannot convert from 'LPTSTR []' to 'LPCTSTR *'

I use Visual Studio 2013 and I get the following error:

error C2664: 'DWORD Options(int,LPCTSTR *,LPCTSTR,...)' : cannot convert argument 2 from 'LPTSTR []' to 'LPCTSTR *' 54 1 ConsoleApplication3

This is the code:

DWORD Options(int argc, LPCTSTR argv[], LPCTSTR OptStr, ...){
    // Code
}
int _tmain(int argc, LPTSTR argv[]){
   iFirstFile = Options(argc, argv, _T("s"), &dashS, NULL);
   // Code 
}

Does anyone know how to fix it?
And explain why this error does occur?

Upvotes: 0

Views: 1208

Answers (1)

zdf
zdf

Reputation: 4808

"And explain why this error does occur?"

The reason behind this error can be found here: an implicit conversion "... would let you silently and accidentally modify a const object without a cast..."

"Does anyone know how to fix it?"

LPCTSTR argv[] is not a constant object, but an array of constant strings. The array itself may be modified (argv[0] = 0;). Since the advice in the link above is to avoid casting ("...please do not pointer-cast your way around that compile-time error message..."), the simplest solution is to change the signature of Options (notice the added const):

DWORD Options(int argc, const LPCTSTR argv[], LPCTSTR OptStr, ...)

Upvotes: 4

Related Questions