gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > externally setting structures

#7577 - Psyk - Fri Jun 20, 2003 8:52 am

This should be easy to answer if i explain it right. I have a C file that contains my map data but i also want it to contain certain attributes for that map. I have a structure for these attributes but i cant put the data into the structure outside of the code. GCC accepts the declaration of my structure instance as a const but not the rest of it.

Code:

const Bg rooma;

rooma.sizeX=32;
rooma.sizeY=32;
rooma.number = 2;

It comes up with a parse error before '.' token. I have tried making them constants aswell but the error still comes up

#7587 - Sweex - Fri Jun 20, 2003 11:57 am

If you want to define the data at compiletime, do it like this:

Code:

const Bg rooma = { 32, 32, 2 };


Also the code you gave should not work. You defined Bg as a const, and therefore you are not able to change it contents at runtime.

That's the whole idea of "const" is that you cannot change it. Knowing that this is the case, the compiler can make certain optimizations. On the GBA for instance, it will put it in the ROM instead of working RAM.

Hope this helps!

#7590 - Psyk - Fri Jun 20, 2003 12:11 pm

Well i don't want to change most of the contents at run time so i tried declaring them as costants aswell but it came up with the parse error. I will try your suggestion.

#7615 - MumblyJoe - Sat Jun 21, 2003 5:34 am

If you dont mind using c++ (i know it adds overhead so you may not want to) you can make const objects that are initialised with the constructor. In fact, you dont even have to use the class keyword if it makes you uncomfortable, c++ allows structs to make member functions, the only difference is that the contents of structs are public by default while the contents of classes are private.

So you could make something like this:

Code:
struct Bg
{
  Bg(const int one, const int two, const int three)
  :one(one),two(two),three(three){}
  int one,two,three;
};

const Bg a(5,6,7);


And then it should be unchangeable and initialised.
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!

#7616 - Psyk - Sat Jun 21, 2003 8:32 am

That sounds good. But would that work if not all of the data is constant? would i just not declare those variables as constant?

#7642 - sgeos - Sun Jun 22, 2003 4:54 am

You might want to do something like this:

Code:
struct whatever
{
};

const struct whatever my_whatever1 = {...};

struct whatever current_whatever = {...};


Then you can copy everything from the const version into the non-const version. Do whatever you want with it, including modification, and then load something else into it (or discard it) when you are done with it. If you want me to, I'll give an example.

-Brendan

#7645 - Psyk - Sun Jun 22, 2003 7:43 am

yeah that should work, thanks! In fact i think that would work better than what i was planning on doing before.

#7655 - sgeos - Sun Jun 22, 2003 3:50 pm

Psyk wrote:
yeah that should work, thanks!


You are welcome.

-Brendan