Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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


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


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

umm in winnt.h a LPSTR is defined as a char*

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.


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

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)



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

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.  

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

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'.

RE: How to convert a char* to a 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*.


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

>> 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

int Change( const char* file)
  char* ptr = (char*) file ;
  // now use ptr as a char* parameter

hope that helps

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

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....


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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close