How to change a variable from const char* to c8*?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

How to change a variable from const char* to c8*?

Post by Morgawr »

Hello everyone, I have a little minor problem...

I have gotten from an XML file an attribute as a const char* using the XMLFile->getAttributeValue("attribute_name") and I want to save it into a c8* variable (because that's what I use in my program). How can I convert it? I tried casting it but the "const" attribute to the variable bothers me a little.. I think I lack some C++ basic knowledge for this maybe? I don't know can someone very please help me? :wink:
Bersi
Posts: 53
Joined: Tue Sep 04, 2007 8:23 pm

Post by Bersi »

Do you want to copy the string/data or do you only want to save the pointer?

In first case you can use some C-functions like strncpy or sprintf:

Code: Select all

const char* source = "Hello World";
c8* dest = new c8[100];
strncpy(dest, source, strlen(source)); // way 1
sprintf(dest, "%s", source); // way 2

In second case you can use const c8* instead of c8*. Or you have to use const_cast. In most cases const_cast is an indication for bad code design. This means in most cases you don't need to cast a const char* to a char* because then you can change the pointer, while the const don't allow it.


Btw, c8 and char are equal data types.
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

Bersi wrote:Do you want to copy the string/data or do you only want to save the pointer?

In first case you can use some C-functions like strncpy or sprintf:

Code: Select all

const char* source = "Hello World";
c8* dest = new c8[100];
strncpy(dest, source, strlen(source)); // way 1
sprintf(dest, "%s", source); // way 2

In second case you can use const c8* instead of c8*. Or you have to use const_cast. In most cases const_cast is an indication for bad code design. This means in most cases you don't need to cast a const char* to a char* because then you can change the pointer, while the const don't allow it.
No no I just need to copy the text so I guess I could use that code... Do I have to import some C headers in order to use that?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Better use the string classes core::Stringc and core::Stringw for these purposes. Otherwise you have to cope with all the memory allocation stuff like in plain C.
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

hybrid wrote:Better use the string classes core::Stringc and core::Stringw for these purposes. Otherwise you have to cope with all the memory allocation stuff like in plain C.
ehm? Where do I find those? They are in Irrlicht? :oops:

can you post an example code please? :roll:
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

They're in include/irrString.h. Just include irrlicht.h

Code: Select all

const char * text = "Some text";
irr::core::stringc copyOfText(text); // or just do using irr; using core;
(void)printf("%s\n", copyOfText.c_str());
Of course, stringc::c_str() returns const char *, so I guess you're still boned if you want to avoid casting. :P

Code: Select all

#include <string.h>
const char * text = "Some text";
c8 * copyOfText = strdup(text);

// But at some point you must remember to do this:
free(copyOfText);
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Bersi
Posts: 53
Joined: Tue Sep 04, 2007 8:23 pm

Post by Bersi »

Google is your friend!

sprintf: #include <stdio.h> or better #include<cstdio>
strncpy: #include <string.h> or better #include <cstring>

For C++ strings: #include <string>


Then you can use it:

Code: Select all

const char* Source = "Hello World";
std::string MyString = Source;

If you don't want to add the std:: you have to add the following line one time:

Code: Select all

using namespace std;
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I would use the string class from Irrlicht (I love it), like rogerborg said... ;)
for the future you should also have a look for the array class from Irrlicht, it's prety usefull, too !!! :)
rogerborg wrote:

Code: Select all

#include <string.h>
const char * text = "Some text";
c8 * copyOfText = strdup(text);

// But at some point you must remember to do this:
free(copyOfText);
I don't think this is necessary, because c8 is nothing more than char... :shock:
OK, if a function wants a c8* and the compiler throws an error about passing a char* you'll have to do a type cast, but in my oppinion this is better than this strdup... :?

also with the string class you can create strings of either type:

Code: Select all

string<char> cString;
string<wchar_t> wString;
string<c8> c8String;
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Acki wrote:I don't think this is necessary, because c8 is nothing more than char...
I'm assuming that Morgawr intends to modify the contents of the memory (or else he'd be using const c8 *), and so he has to duplicate the memory returned from XMLFile (which is declared to be const).
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I guess it's best to store and manipulate inside the stringc class... So what are you actually doing with the c8*?
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

I must apologize for my newbiness here now.. :oops:
I took a whole day of lurking in the API and I honestly didn't notice stringc and stringw before in Irrlicht..

I was basically using c8* as a string but since now I've found stringc and stringw, I'm sticking to them! Sorry for this stupid thread, I should have lurked a bit more before asking these kind of questions but I was a bit desperate because my program wasn't running (now it does)

so thank you all for your quick and very interesting answers :wink: :wink:
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Morgawr wrote:I should have lurked a bit more before asking these kind of questions
this is always a good idea !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply