Solved

Initialising const structures

Posted on 2000-02-14
11
326 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:Ghostrider
  • 5
  • 4
  • 2
11 Comments
 

Author Comment

by:Ghostrider
ID: 2521115
ooops, and main should be declared as follows:
int main(int argsc, char *argsv[])
0
 
LVL 3

Accepted Solution

by:
ufolk123 earned 200 total points
ID: 2521241
Here is the solution:

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


int main(int argsc, char *argsv)
{
      const andy andy_b;
      const andy andy_a;

      andy* temp;

                /* Do assignment 1*/
      temp=(andy*)&andy_b;
      temp->age=22;
      temp->brother=(andy*)&andy_a;

                /* Do assignment 2*/
      temp=(andy*)&andy_a;
      temp->age=22;
      temp->brother=(andy*)&andy_b;
      
      
}                        }
0
 

Author Comment

by:Ghostrider
ID: 2524335
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:aperdon
ID: 2525090
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.
0
 

Author Comment

by:Ghostrider
ID: 2525322
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
0
 
LVL 1

Expert Comment

by:aperdon
ID: 2525345
Is it a big deal when they aren't const?
0
 

Author Comment

by:Ghostrider
ID: 2525363
If the optimisation is into read-only memory, then very much s!
0
 
LVL 1

Expert Comment

by:aperdon
ID: 2525397
Why?
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2525527
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.

0
 
LVL 1

Expert Comment

by:aperdon
ID: 2528369
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.
0
 

Author Comment

by:Ghostrider
ID: 2531191
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using unmanaged DLL from managed application 14 297
convert char array to number in c 5 91
valid enum? 6 76
Raspberry Pi 3 to send text message 9 197
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question