const structs in other structs

Discussion in 'C Programming' started by Chris Hauxwell, Apr 23, 2004.

  1. I think the following code should give an error when compiled,
    however, the compiler I am using doesn't raise one. The code is:

    typedef struct
    {
    char a1;
    char a2;
    char a3;
    } A_T;

    typedef struct
    {
    char b1;
    const A_T ba;
    } B_T;

    B_T b;

    void main(void)
    {
    b.ba.a1 = 0;
    }

    Should this give an error? I've declared the A_T part of B_T as a
    const and then written to it in main(). I think it should, but my
    compiler is happy with it. Any comments will be appreciated.

    Thanks

    Chris
    Chris Hauxwell, Apr 23, 2004
    #1
    1. Advertising

  2. (Chris Hauxwell) wrote:
    >I think the following code should give an error when compiled,
    >however, the compiler I am using doesn't raise one. The code is:
    >
    >typedef struct
    >{
    > char a1;
    > char a2;
    > char a3;
    >} A_T;
    >
    >typedef struct
    >{
    > char b1;
    > const A_T ba;
    >} B_T;
    >
    >B_T b;
    >
    >void main(void)
    >{
    > b.ba.a1 = 0;
    >}
    >
    >Should this give an error? I've declared the A_T part of B_T as a
    >const and then written to it in main(). I think it should, but my
    >compiler is happy with it. Any comments will be appreciated.


    A decent compiler should emit two diagnostics: one about the
    inappropriate declaration of the main function, and another one
    about assignment to a read-only object. You should check with
    the documentation how to invoke the compiler in conforming mode.

    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
    Irrwahn Grausewitz, Apr 23, 2004
    #2
    1. Advertising

  3. Chris Hauxwell

    Kevin Bracey Guest

    In message <>
    (Chris Hauxwell) wrote:

    > I think the following code should give an error when compiled,
    > however, the compiler I am using doesn't raise one. The code is:
    >
    > typedef struct
    > {
    > char a1;
    > char a2;
    > char a3;
    > } A_T;
    >
    > typedef struct
    > {
    > char b1;
    > const A_T ba;
    > } B_T;
    >
    > B_T b;
    >
    > void main(void)
    > {
    > b.ba.a1 = 0;
    > }
    >
    > Should this give an error?


    Absolutely. Qualifiers are cumulative within structures, so if either b.ba or
    b are explicitly declared const, then b.ba.a1 has type const char.

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
    Kevin Bracey, Apr 23, 2004
    #3
  4. Chris Hauxwell

    myName Guest

    Chris Hauxwell wrote:
    >
    > I think the following code should give an error when compiled,
    > however, the compiler I am using doesn't raise one. The code is:
    >
    > typedef struct
    > {
    > char a1;
    > char a2;
    > char a3;
    > } A_T;
    >
    > typedef struct
    > {
    > char b1;
    > const A_T ba;
    > } B_T;
    >
    > B_T b;
    >
    > void main(void)
    > {
    > b.ba.a1 = 0;


    left operand must be modifiable lvalue: op "="

    > }


    Which complier u using? Theoretically compiler shld give error.

    Ajay
    myName, Apr 23, 2004
    #4
  5. Chris Hauxwell

    Guillaume Guest

    > Absolutely. Qualifiers are cumulative within structures, so if either b.ba or
    > b are explicitly declared const, then b.ba.a1 has type const char.


    Yes, although I haven't heard of const struct members in C.
    Have I missed anything?
    Guillaume, Apr 23, 2004
    #5
  6. Chris Hauxwell wrote:

    > I think the following code should give an error when compiled,
    > however, the compiler I am using doesn't raise one. The code is:
    >
    > typedef struct
    > {
    > char a1;
    > char a2;
    > char a3;
    > } A_T;
    >
    > typedef struct
    > {
    > char b1;
    > const A_T ba;
    > } B_T;
    >
    > B_T b;
    >
    > void main(void)
    > {
    > b.ba.a1 = 0;
    > }
    >
    > Should this give an error? I've declared the A_T part of B_T as a
    > const and then written to it in main(). I think it should, but my
    > compiler is happy with it. Any comments will be appreciated.


    1) It is an error to try to assign to a const member
    2) It is not valid to declare main as having a type of void
    3) It is in C before C99 -- which I doubt you have -- an error not to
    explicitly return a value from main
    4) It is silly to run your compiler with diagnostics turned off. It is
    even sillier to do so and then take the absence of diagnostics as being
    meaningful.
    Martin Ambuhl, Apr 23, 2004
    #6
  7. (Chris Hauxwell) wrote in message news:<>...
    > I think the following code should give an error when compiled,
    > however, the compiler I am using doesn't raise one. The code is:
    >
    > typedef struct
    > {
    > char a1;
    > char a2;
    > char a3;
    > } A_T;
    >
    > typedef struct
    > {
    > char b1;
    > const A_T ba;
    > } B_T;
    >
    > B_T b;
    >
    > void main(void)
    > {
    > b.ba.a1 = 0;
    > }
    >
    > Should this give an error? I've declared the A_T part of B_T as a
    > const and then written to it in main(). I think it should, but my
    > compiler is happy with it. Any comments will be appreciated.
    >
    > Thanks
    >
    > Chris



    Thanks for everyones comments. I'm happy now that this code should
    give an error message. Just some comments on some of the comments I
    received.

    1. Good point about main being void rather than int. I should have
    mentioned that I write code for embedded real-time systems where
    main() normally never returns due to an infinite loop. Many compilers
    (particularly those for embedded systems) will not complain about
    this.

    2. I've tried this code on many different compilers and they all
    return either an error or warning about the const value being written
    to. Only the compiler I am using doesn't.

    Thanks

    Chris Hauxwell
    Chris Hauxwell, Apr 27, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,082
  2. Javier
    Replies:
    2
    Views:
    532
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,079
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    862
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    517
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page