Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BeanDog

Passing in LPSTR's for char*'s

This topic is 6943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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)

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Or you could simply write:

LPSTR string = "What_Ever";
SomeFn(string);

-EOF

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 **

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!