Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert a char* to a LPSTR???

Status
Not open for further replies.

rhpingwoo

Programmer
Mar 5, 2003
15
0
0
AU
Hello,

Does anybody know how to convert a varaiable with type char*
to a type "LPSTR"???

Thanks,
raph.
 
umm in winnt.h a LPSTR is defined as a char*
Code:
typedef char CHAR;
typedef CHAR *LPSTR, *PSTR;
do you get a compiler error? post it and the line of code the error occurs on and the variable declarations for any variables in that line of code.

-pete
 
Just make it a bit more clear.
I had a parameter pass it to a fuction and its type is const char*, and that parameter contain the path and the filename of a file. Is it possible for me to change that to LPSTR??

For example

int Change( const char* file)
{



}
 
Like palbano said, LPSTR is a char*. Just send the char*, you won't have any problems if the char* is correctly formatted for the function.
 
what if now i want to convert a

const char* to LPSTR???

It give me error message saying that

cannot convert parameter 1 from 'const char *' to 'LPSTR'.
 
That's correct, that would just be like casting a const char* to a char*, which is not possible. You see, a const char* is meant to be a not "editable" array of characters (const pointers are pointing to const objects). If it would be as simple as just casting it to an ordinary char*, there wouldn't be much point in having a const char*.
Greetings,
Rick
 
>> cannot convert parameter 1 from 'const char *'
>> to 'LPSTR'.

Well it's not so much that it "can't" as it "won't". For obvious reasons it is considered "bad" practice to convert const pointers to non-const pointers. Therefore the compiler will not provide automatic conversions.

You can do them yourself, but you better know what your doing if you use this often. This is an area of programming, especially C/C++, where actually studying the subject becomes very important. I seriously suggest all C++ developers read “More Effective C++” by Myers.

Anyway to the point at hand
Code:
int Change( const char* file)
{
  char* ptr = (char*) file ;
  // now use ptr as a char* parameter
}

hope that helps
-pete
 
Yep palbano is right. You can always explicitly cast it yourself, but that's real bad practice. In most cases the const modifier is used when pointers are needed as a function argument (like in your 'Change' example). When the caller of such a function sees that a const char* is expected he can rest assured that the memory block he's passing won't be modified by the function. Or at least he should be able to be rest assured of that. If some malicious programmer decides to explicitly cast the pointer and still change the memory block, that would be real bad practise at the least....
Greetings,
Rick
 
If I may add one more little note, rather than using C-style casting, which may be difficult to locate after you've decided the casting is causing an error, consider using the reinterpret_cast, const_cast, and static_cast operators. That way, when you have to find your mistake, it will be easier to search for places where you did this.

Scott Meyers made an excellent point in stating something like "if you're going to do this bad thing, it should at least be hard to type". I will admit, though, sometimes you must do the bad thing to get the job done...that's why the ability to do so is there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top