Link to home
Start Free TrialLog in
Avatar of Ghostrider
Ghostrider

asked on

Initialising const structures

Hi I have a problem:

I need to initialise two structures, that reference each other and yet are declared as const:

consider:

typedef struct _andy {
int age;
struct _andy *brother
}andy;

I can't do:

int main(int argsc, char *argsv)
{
  const andy andy_a;
  const andy andy_b;
  andy_a = {
     age : 22,
     brother : &andy_b
     }
  andy_b = {
     age : 22,
     brother : &andy_a
     }
}

Because that's not valid initialisation. Is there anyway that I can initialise these structures so that they are const?

Cheers

Andy
Avatar of Ghostrider
Ghostrider

ASKER

ooops, and main should be declared as follows:
int main(int argsc, char *argsv[])
ASKER CERTIFIED SOLUTION
Avatar of ufolk123
ufolk123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That looks great, you've got the points,
the question is though, will this maintain the 'const' and keep the optimisations the compiler makes due to the 'const' declaration?

Cheers

Andy
I donot agree the structure must be const. Why? Because it is not true that your brother stays the same all live. He can die. Also, how about adding brothers. This is not correct OO, and maybe that is the reason why you encouter this kind of problem.
Aperdon,

Thanks for the comment, however this is
a toy example, and I have not explained
what I am really trying to do because it is more complex... it does however amount to this toy example. What I have said, is what I am trying to achieve.
Anyway, this is in C, I'm not attempting OO stuff, I just need two const structures that reference each other. Now if the compiler was intelligent enough to place these structures in read only memory as an optimisation, then I *want* this done, but I need those references set. This is what I am trying to achieve, and I want to know if it's possible... I'm still quite sceptical.

Cheers

Andy
Is it a big deal when they aren't const?
If the optimisation is into read-only memory, then very much s!
Why?
Sure it will maintain the logical const -ness of the structures .( You won't be able to do direct assignments).
Nobody can stop you in modifying a const variable by directly manipulating it using a pointer to its address in C.
But I am not sure about the compiler optimisations issues for const data type.May be your point is allocation of  storage for const data objects  in read-only memory etc.That may depend on  type of compiler and env your are using.

For me I don't understand this whole const-commotion. I donot use const anymore since 4 years ago or so. Const is nice for documenting your code, but a crime to maintain and to be consistent. What about this terrible const in C++ when using pointers.
Right I'm going to wrap this one up,
actually, I found a better way to achieve what I wanted to do using forward prototypes. Yes, the optimisations are platform specific, and why do I need them - I'm working on a runtime system that needs to be as quick as possible.

Cheers for your help and comments guys!

later

Andy