Passing in LPSTR's for char*'s
I have a little procedure that takes a char* parameter and changes around that array. Take a look at this demi-code:
void SomeFn(char* string)
{
string[0] = ''h'';
string[1] = ''e'';
return;
}
int main()
{
LPSTR string;
SomeFn(string);
}
This boots me from the program when it is called. Shouldn''t this work?
~BenDilts( void );
You aren''t allocating memory for the string. LPSTR string will only give you a pointer to a char. You still need to do something like string = new char[10]; (or whatever)
I changed it so that I initialize the string like:
LPSTR string = new string[100];
But when I do this:
string = "Junk";
and then call the function, it still boots me. I CAN call the function if I don''t do string = "junk"; though. That probably sets string up as a pointer to the constant string "junk". Is there a way to do this that will still let me call the function?
~BenDilts( void );
LPSTR string = new string[100];
But when I do this:
string = "Junk";
and then call the function, it still boots me. I CAN call the function if I don''t do string = "junk"; though. That probably sets string up as a pointer to the constant string "junk". Is there a way to do this that will still let me call the function?
~BenDilts( void );
If you want to initialize the string in your main function to "junk" then you should do something like
LPCSTR string = new char[100];
strcpy("junk", string);
LPCSTR string = new char[100];
strcpy("junk", string);
quote:Original post by Anonymous Poster
Or you could simply write:
LPSTR string = "What_Ever";
SomeFn(string);
-EOF
Uh huh. Then you get an access violation as you try to write in a read-only data area. You need to allocate the memory if you plan to write in it.
quote:
Uh huh. Then you get an access violation as you try to write in a read-only data area. You need to allocate the memory if you plan to write in it.
Damn I din´t see that he wrote into the memory, so you are 100% right, this will give a nasty AV (access violation ).
Sorry my fault, it must read with care.
** The AV-guy **
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement