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?
How to change a variable from const char* to c8*?
How to change a variable from const char* to c8*?
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?
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?
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:
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.
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 2In 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.
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?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.
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
They're in include/irrString.h. Just include irrlicht.h
Of course, stringc::c_str() returns const char *, so I guess you're still boned if you want to avoid casting. 
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());
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
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:
If you don't want to add the std:: you have to add the following line one time:
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;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 !!!
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:
for the future you should also have a look for the array class from Irrlicht, it's prety usefull, too !!!
I don't think this is necessary, because c8 is nothing more than char...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);
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:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

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:
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).Acki wrote:I don't think this is necessary, because c8 is nothing more than char...
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
I must apologize for my newbiness here now..
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

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
this is always a good idea !!!Morgawr wrote:I should have lurked a bit more before asking these kind of questions
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java