Re: can't initialize a constant using another constant?
Glynn Clements <[email protected]>
| Newsgroups | org.kernel.vger.linux-c-programming |
|---|---|
| Message-ID | <[email protected]> |
Shriramana Sharma wrote:
> Thanks to all those who replied. I am very sorry I did not specify the
> compiler version etc. I should have. It's gcc version 4.1.3 20070929
> (prerelease) (Ubuntu 4.1.2-16ubuntu2).
>
> Glynn Clements wrote:
> > In C, "const" is only relevant to pointer targets. Adding the "const"
> > modifier to a variable has no effect.
>
> I don't understand what you mean. I just tried gcc -o foo foo.c on:
>
> # include <stdio.h>
> main () {
> const int i = 1 ;
> i = 2 ;
> printf ( "%d\n", i ) ;
> }
>
> and I got:
>
> foo.c: In function �main�:
> foo.c:6: error: assignment of read-only variable �i�
>
> So in what sense are you saying adding const to a variable has no effect?
Sorry; my mistake.
However, although the compiler will prevent modification to const
objects (and if the compiler didn't, the CPU will, as they are stored
in the .rodata section, which is mapped read-only), they are still
considered variables rather than (compile-time) constants, and can't
be used in initialisers or in array dimensions.
In C++, const-qualified objects are treated as compile-time constants.
--
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html