const structs in other structs

C

Chris Hauxwell

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
 
I

Irrwahn Grausewitz

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
 
K

Kevin Bracey

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.
 
M

myName

Chris said:
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
 
G

Guillaume

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?
 
M

Martin Ambuhl

Chris said:
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.
 
C

Chris Hauxwell

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
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top