[PLUG] C question on "const int"

Dean S. Messing deanm at sharplabs.com
Fri May 13 20:43:05 UTC 2005


: Dean S. Messing wrote:
: > I had understood that if, in C, you do something like
: > 
: >   const int i=5;
: > 
: >   i=10;
: > 
: > that this was an error.  But when I compile the code I merely
: > get a warning:
: > 
: >    warning: assignment of read-only variable `i'
: > 
: > and, worse, it reassigns i to have value 10.
: > 
: > How does one make the compiler elicit a hard error and stop the
: > compilation?  This is using gcc-3.3.2, by the way.
: > 
: > Dean
: 
: Possible answers:
: 
:   1. Use gcc-3.4. (I just tried the above and it produced an error).

Sure Enough!  I ran it on my Mandriva 2005LE machine
and I got an error.


:   2. Compile with "-Werror".  Will turn all warnings (including that one)
:      into errors.

Too strong. There are lots of innocuous warnings.

:   3. use "#define" rather than "const int" (local non-pointer constants
:      seem a little silly to me...)

The example I provided was a simplification of
the actual situation.

:   4. there might be another command-line option, but since I can't get
:      gcc-3.4 to take that code at all, I don't know what it might be.

Thanks.  Going to 3.4 fixes the issue.

Dean



More information about the PLUG mailing list