C - Const Struct Members

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mas

    #1

    C - Const Struct Members

    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?


  • Eric Sosman

    #2
    Re: C - Const Struct Members

    Mas wrote:[color=blue]
    > 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?[/color]

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

    --
    Eric Sosman
    esosman@acm-dot-org.invalid


    Comment

    Working...