Re: const issues

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

 



Hi Jason,

your code isn't valid C. A global variable can only be initialized
with a compile-time constant, and a const variable isn't a
compile-time constant in C. Compare with the following program:

#include <stdlib.h>

const int a = 42;
const int b = a; // Error

int main()
{
   return 0;
}

See e.g. http://c-faq.com/ansi/constasconst.html

By contrast, in C++ a global const integral variable is a compile-time
constant, and you can use it as an initializer.

   Dario

2008/2/29, Jason Pepas <jasonpepas@xxxxxxxxx>:
> this compiles as expected:
>
>
>  #include <stdlib.h>
>
>  int main()
>  {
>     const int a = 42;
>     const int * const b = &a;
>     const int * const c = b;
>
>     return EXIT_SUCCESS;
>  }
>
>
>  but moving the variables outside of the function causes gcc to die
>  with "error: initializer element is not constant" on line 5 (the "c =
>  b" line):
>
>
>  #include <stdlib.h>
>
>  const int a = 42;
>  const int * const b = &a;
>  const int * const c = b;
>
>  int main()
>  {
>     return EXIT_SUCCESS;
>  }
>
>
>  As best as I can tell, the error is a lie: I can't make that  any more
>  const than it already is.
>
>  ideas?
>
>
>  -jason pepas
>

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux