the "const" qualifier

Doug Gwyn gwyn at smoke.BRL.MIL
Wed Aug 1 23:44:32 AEST 1990


In article <1990Aug1.005200.21645 at ccu.umanitoba.ca> rpjday at ccu.umanitoba.ca writes:
-  Section 3.5.3, line 26, we have, "For two qualified types to be
-compatible, both shall have the identically qualified version of a 
-compatible type..."  Does "compatible" mean assignment compatible?

No, "compatible type" is a technical term defined in section 3.1.2.6.

-That is, I am not allowed to do 
-	const int i = 10;
-	int j;
-	j = i;   ???
-even though this has no effectg on the value of j?

I don't understand your example.  Certainly it is allowed, and the
value of j is definitely affected (it is set to 10).

-  This is just the beginning.  At the top of the next page, in the
-segment of code, line 7 shows an assignment of a const-qualified
-structure to a non-const-qualified structure.  But based on the
-previous definition, should this work?  The comment suggests it should.

Of course it should work.  What argumentation would you offer for it
being disallowed?

You cannot store INTO a const-qualified object; however, you may
pick up its contents!

-  Finally, line 12 of 3.5.3, "If an attempt is made to modify an object
-defined with a const-qualified type through use of an lvalue with
-non-const-qualified type, the behavior is undefined."  How would one
-do this anyway?

	const int j = 10;
	int *p = (int *)&j;
	*p = 20;

- Maybe as follows?
-	const int j = 10 ;
-	int i ;
-	*(&i + 1) = 20 ;

Yuck!  That's horrible code, which one has no reason to expect to work.



More information about the Comp.std.c mailing list