Re: can't initialize a constant using another constant?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <glynn@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux