C - Const Struct Members

M

Mas

I have 3 different structures with each structure having the same first
member (int type). I would like every structure of type 1 to have a 1 in the
type field, every structure of type 2 to have 2 in the type field etc. I
want instances of each structure to have the type value properly
initialized, and I also want that member to be const. Basically, I want 1
member of a structure to be initialized to a value and be unchangeable. Is
there a way to do this in C?
 
E

Eric Sosman

Mas said:
I have 3 different structures with each structure having the same first
member (int type). I would like every structure of type 1 to have a 1 in the
type field, every structure of type 2 to have 2 in the type field etc. I
want instances of each structure to have the type value properly
initialized, and I also want that member to be const. Basically, I want 1
member of a structure to be initialized to a value and be unchangeable. Is
there a way to do this in C?

struct {
const int tag;
double trouble;
} a = { 1, 42.0 };
...
a.trouble = 3.14; /* allowed */
a.tag = -1; /* not allowed */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top