copy const c8 * to const 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
Lo_Ord
Posts: 12
Joined: Thu Apr 27, 2006 2:24 pm

copy const c8 * to const c8*

Post by Lo_Ord »

Hi!

I know, this is a stupid question, but I've been sitting now on this (ridiculously small) problem and don't know the answer...

Here is what i got:

Code: Select all

struct FireData
{
   //stuff 
   const c8* filename;
}

//in the main function:
{
   FireData fire = new FireData();
   //reading a xml-file
   {
      if (stringw("fire20") == mapXml->getNodeName())
      {
         fire->file = mapXml->getAttributeValue("file");
      }
   }

   //Loading a fireemitter
   //Loading the texture for the emitter:
   fire->node->setMaterialTexture(0, driver->getTexture(filename));

   //do the rest
}
The problem is: when the fireemitter is loaded and I try to load the texture, the const c8* string in fire->file is not valid anymore.

I think I know how to solve the problem: by simply using a strcpy-like funktion, which copy's from const char* to const char* :D

Is there a function or a way to do that?

TiA

Lo_Ord
deesine
Posts: 104
Joined: Fri May 12, 2006 9:19 am

Post by deesine »

I'm taking a pitch at this, even though I'm no expert in keywords like const :oops:

As far as I know, a const variables' value can be defined before compile, but if it's to be changed at runtime, I only think it can reference another variable. That might have to do with why it gets lost.

I probably would have done (pseudo-code):

Code: Select all

struct FireData
{
   //stuff
   c8* filename;
}

//in the main function:
{
   FireData fire;
   //reading a xml-file
   {
      if (stringw("fire20") == mapXml->getNodeName())
      {
         fire.file = (char*)mapXml->getAttributeValue("file");
      }
   }

   //do the rest
}
Shooting myself in the foot probably, I'm interested in reading what more experienced coders have to say on the topic :)
Last edited by deesine on Tue May 30, 2006 12:55 pm, edited 1 time in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Err, this is not named 'const' without intentions. It's simply constant so you should not be able to change it. Const pointers are usually used as parameters showing that the values pointed to are not changed by the method. If you really want to use c8* you have to use strncpy or even better strdup to create the memory on the fly. Otherwise use core::stringc.
deesine
Posts: 104
Joined: Fri May 12, 2006 9:19 am

Post by deesine »

I'll second the use of stringc, I prefer that over direcly specifying c8.
Lo_Ord
Posts: 12
Joined: Thu Apr 27, 2006 2:24 pm

Post by Lo_Ord »

Thx hybrid, the strdup-function did the trick!

I have to keep the const c8*, because I'm reading a texture-filepath from the X-File and load that file directly by using getTexture (see my code-example)

Big thx!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You don't have to use a const c8* because every c8* can become const c8*. This conversion is done autmatically, e.g. when passing this pointer to a const param method. The other way round is not possible without explicitly casting. And you can easily get a const c8* from a core::stringc with the .c_str() method.
Lo_Ord
Posts: 12
Joined: Thu Apr 27, 2006 2:24 pm

Post by Lo_Ord »

hybrid wrote:You don't have to use a const c8* because every c8* can become const c8*. This conversion is done autmatically, e.g. when passing this pointer to a const param method.
Intresting, it was the first thing I tried, and the compiler gave me the error "cannot convert from const c8 * to c8 *"...
hybrid wrote:And you can easily get a const c8* from a core::stringc with the .c_str() method.
Great, thx. Will use it in the future
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Lo_Ord wrote:
hybrid wrote:You don't have to use a const c8* because every c8* can become const c8*. This conversion is done autmatically, e.g. when passing this pointer to a const param method.
Intresting, it was the first thing I tried, and the compiler gave me the error "cannot convert from const c8 * to c8 *"...
No, you cannot convert from const to non-const, only the other way round. This is pretty forward if you take the intention mentioned some posts before (jus marking some parameters to be unchanged). If you pass a non-const variable to a const parameter it's ok. But if you would pass a const variable to a non-const parameter chances are good that the method would try to alter the const variable. And that's obviously an error.
Post Reply