[comp.lang.c] volatiles/consts inside structs in dpANS C

rdh@myrias.UUCP (Dale Hagglund) (03/03/88)

What does the draft Standard have to say about a structure declared as

        struct {
            const int x;
            int y;
            double z;
        } s1, s2;

Are s1 and s2 constant structures, non-constant structures, or are they
something sort of in between?

If they are constant structures, then

	s1.y = s2.y;

becomes illegal, which seems sort of odd. However, if they are not
constant, then

	s1 = s2;

should be legal.  This is equivalent (it is equivalent, right?) to

	s1.x = s2.x;
	s1.y = s2.y;
	s1.z = s2.z;

Unfortunately, the first assignment here is illegal, since s1.x is a
constant field.

I have been looking at my copy of the draft Standard (November 9,
1987), but it doesn't seem to say anything about this particular kind
of declaration.  Section 3.5.3, "Type qualifiers," says only that
members of qualified aggregates or unions have the "equivalently
qualified" types.  It says nothing about whether or not qualification
moves up from aggregate members to aggregates.

So, what's the answer?  Is structure assignment disallowed for those
structs containing const fields, or does the struct assignment just
assign across the non-const fields, making

	s1 = s2;

equivalent to

	s1.y = s2.y;
	s1.z = s2.z;

and just leaving s1.x alone?

Similar sorts of weirdness arise when you replace "const" with
"volatile" in the declaration of s1 and s2 above.  I would appreciate
any comments on just what these sorts of declarations mean, or if they
mean anything at all, either via email or on the net.

Thanks in advance,
	Dale.
--
Dale Hagglund        Phone: (403) 428-1616        UUCP: ...!alberta!myrias!rdh
Myrias Research Corp., 900 - 10611 98th Ave, Edmonton, Alberta, Canada T5K 2P7
-- 
--
Dale Hagglund        Phone: (403) 428-1616        UUCP: ...!alberta!myrias!rdh
Myrias Research Corp., 900 - 10611 98th Ave, Edmonton, Alberta, Canada T5K 2P7

henry@utzoo.uucp (Henry Spencer) (03/13/88)

As I recall (my copy isn't handy), the latest draft says that const,
volatile, etc. inside a struct sort of pop to the surface when you attempt
something like an assignment.  So assigning to a struct with a const
member is illegal.  Personally, I think such "buried qualifiers" should
simply be illegal, since they are useless...
-- 
Those who do not understand Unix are |  Henry Spencer @ U of Toronto Zoology
condemned to reinvent it, poorly.    | {allegra,ihnp4,decvax,utai}!utzoo!henry

karl@haddock.ISC.COM (Karl Heuer) (03/15/88)

In article <1988Mar13.005411.12592@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>... So assigning to [an entire] struct with a const member is illegal.
>Personally, I think such "buried qualifiers" should simply be illegal, since
>they are useless...

Doesn't sound useless to me.  `struct {int; int const;}` has properties
different from either `struct {int; int}` or `struct {int; int} const`: it
gives you a struct in which one member is modifiable and the other is not.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint