Sep 24, 2012 at 6:24pm UTC
Hi,
I am new to C++ coding. I am trying to convert Char * to Const Char[] but I am not able to do it.
Here is the snippet
bool Method (Char *location)
{
static const char FileName[]= strcat(location, "\\Root");
}
I am getting this error:
error C2440: 'initializing' : cannot convert from 'char *' to 'const char []'
Can someone please explain and correct me.
Thanks in advance
Sep 24, 2012 at 6:46pm UTC
That's not how strcat works, which you don't use in C++ anyway. The correct version looks like this:
1 2 3 4 5
bool method(std::string location)
{
const std::string fileName=location+"\\Root" ;
[...]
}
Are you sure you know what static does?
Last edited on Sep 24, 2012 at 6:47pm UTC
Sep 24, 2012 at 7:08pm UTC
Thanks for your replies cire and Athar.
I tried the way suggested by Cire and it worked but the problem is after I get the FileName I need to pass it to a Method whose input argument is a Char *. Since the FileName will be of type std::string it is giving me an error
error C2664: : cannot convert parameter 1 from 'const std::string' to 'const char *'
Sep 24, 2012 at 7:23pm UTC
Use method of class std::string c_str()
For example
std::string s( "Tets" );
std::cout << s.c_str() << std::endl;
Sep 24, 2012 at 7:38pm UTC
Thanks Vlad, I used c_str() to convert std::string to char * and was able to achieve what I wanted to do.
Thank you guys (cire and Athar)
Sep 24, 2012 at 7:48pm UTC
You may not use c_str() as an argument for a parameter of type char *. The parameter shall be declared as const char *
Last edited on Sep 24, 2012 at 7:48pm UTC