bi**********@gm ail.com wrote:
Hi!
I have some problems in the following code.
#include <windows.h>
#include<iostre am.h>
void main(){
LPSTR x="";
long y=125;
_ltoa(y, x, 10);
cout<<x<<endl;
}
LPSTR is a synonym for char*, don't ask me why they decided to create
typedefs for standard types. What your x is is actually a char const*
but the compiler doesn't warn you about this kind of const assignment
(imho this is a weakness of the language, it should complain). _ltoa
is a "standard compliant" version of the posix ltoa...that's MS wordage
there, it's BS.
So anyway, what you are trying to do is modify data stored in constant
memory through a non-constant point. This results in undefined
behavior, this type of undefined behavior often shows itself as a crash
or access violation.
You should use the standard types and function names instead of the MS
verbage. You can turn off the influx of warnings you'll get by using
ltoa with a macro definition and the other type is just a typedef so it
won't even turn up. The standard types are more descriptive and won't
get you heckled when you post in a c++ group or forum. So learn what
the typedefs are for DWORD, LONG, LPSTR, LPCSTR, etc, and use the
original type instead of the MS specific alias. This has the added
benefit of making your code more portable, which MS is trying to avoid
by tricking you into using them.