Total char * confusion

Started by
13 comments, last by Attala101 18 years ago
Hi I'm using ID3DXFont to print som basic text on the screen. All goes well when I do: char strBuffer[255]; sprintf_s(strBuffer,"Movingnation: %i", movingnation); MyFont->Print(strBuffer, 0, 40); I have a class (MyText) which among other things have a char * member (newtext). When I try this: char strBuffer[255]; sprintf_s(strBuffer,"Country: %s", nations[movingnation]->sCountryName); MyText->newtext = strBuffer; It print some garbled nonsense. I understand that we're talking about different formats of strings but I just don't know how to solve this problem. After googling for an hour I'm still lost, so any help would be nice :) A
Advertisement
First are you using C or C++, if you are using C++ switch to std::string. You could have many problems for example if your code looks like this:
TextClass MyText;void func(){    char strBuffer[255];    sprintf_s(strBuffer,"Country: %s", nations[movingnation]->sCountryName);    MyText->newtext = strBuffer;}

This will introduce a problem because your variable newtext only points to strBuffer, and when we exit the function func strBuffer is getting destructed and newtext will point to a destroyed string. You should use strcpy to copy strings, anyway without more code I doubt anyone can guess your error.
a char * is NOT a string. It's a pointer to a place in memory where a character lives. When you assign newtext to be strBuffer all you've done is assign newtext to point to the memory location on the stack where strBuffer lives. As soon as you leave the scope in which strBuffer was declared your newtext pointer points to what is now a random point in mempory

to reiterate this statement does NOT copy the string. it copies a memory pointer:

MyText->newtext = strBuffer;

the correct way to copy char* is like this:

//i think you need the +1 for the \0 character...
MyText->newtext = new char[ strlen(strBuffer)+1 ];
strcpy(myText->newtext, strBuffer);

now you have actually copied the string. but now you have to remember to deallocate the memory when you are done with it:

delete[] MyText->newtext

if you want simple strings use the std::string class. To use char* strings without destroying your program you MUST understand pointers and how to use them.

-me
--- Comments Removed ---

I messed up on the respones, sorry. = ]
Ok, I see my error. I still keep confusing the use of pointers from time to time :) Another day in the life of a newbie :)

I solved the problem by making strBuffer a member of my MyText class. That way there's still something to point at when I'm exiting the function. There are probably many better solutions to this, but it works for now.

Thanks guys!

A
Quote:Original post by Attala101
Ok, I see my error. I still keep confusing the use of pointers from time to time :) Another day in the life of a newbie :)

I solved the problem by making strBuffer a member of my MyText class. That way there's still something to point at when I'm exiting the function. There are probably many better solutions to this, but it works for now.

Thanks guys!

A


I strongly urge you to reconsider. You _will_ have more of these problems in the future if you continue to use raw char arrays over std::string.

Seriously, use std::string, it's soo much easier.

Quote:Original post by rip-off
Quote:Original post by Attala101
Ok, I see my error. I still keep confusing the use of pointers from time to time :) Another day in the life of a newbie :)

I solved the problem by making strBuffer a member of my MyText class. That way there's still something to point at when I'm exiting the function. There are probably many better solutions to this, but it works for now.

Thanks guys!

A


I strongly urge you to reconsider. You _will_ have more of these problems in the future if you continue to use raw char arrays over std::string.

Seriously, use std::string, it's soo much easier.


Ok, I will :) I just thought a std::string could not be passed directly to the ID3DXFont DrawText function, (don't ask me why). Anyway, that is why I built the whole thing around char arrays.

Thanks for the advice.

A
Quote:Original post by Attala101
Ok, I will :) I just thought a std::string could not be passed directly to the ID3DXFont DrawText function, (don't ask me why).


It can't, but std::string::c_str() can.
Ok, now I have converted all my char arrays to std::string. With the use of c_str() most of it compiled fine. I am left with one error though.

MyFont->Print(MyText->infotext.c_str(), MyText->xpos, MyText->ypos, opacity );

the Print function in my Fontclass calls the DrawText function.

I'm getting the error: error C2664: 'Fontclass::Print' : cannot convert parameter 1 from 'const char *' to 'char *'
Quote:Original post by Attala101
Ok, now I have converted all my char arrays to std::string. With the use of c_str() most of it compiled fine. I am left with one error though.

MyFont->Print(MyText->infotext.c_str(), MyText->xpos, MyText->ypos, opacity );

the Print function in my Fontclass calls the DrawText function.

I'm getting the error: error C2664: 'Fontclass::Print' : cannot convert parameter 1 from 'const char *' to 'char *'


The Fontclass is your own right? You need to make the first parameter of type const char*, which shows that the print function won't actually modify the string. This is a requirement for std::string, you can only access the c_str as read-only (const). Anyway since this function is most likely your own you should also use std::string here and not call c_str until you are passing it to a third-party function using const char*.

This topic is closed to new replies.

Advertisement