public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* const issues
@ 2008-02-29  8:07 Jason Pepas
  2008-02-29  8:13 ` Dario Saccavino
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Pepas @ 2008-02-29  8:07 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: const issues
  2008-02-29  8:07 const issues Jason Pepas
@ 2008-02-29  8:13 ` Dario Saccavino
  2008-02-29 15:14   ` Jason Pepas
  0 siblings, 1 reply; 3+ messages in thread
From: Dario Saccavino @ 2008-02-29  8:13 UTC (permalink / raw)
  To: Jason Pepas; +Cc: gcc-help

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@gmail.com>:
> 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
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: const issues
  2008-02-29  8:13 ` Dario Saccavino
@ 2008-02-29 15:14   ` Jason Pepas
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Pepas @ 2008-02-29 15:14 UTC (permalink / raw)
  To: Dario Saccavino; +Cc: gcc-help

On Fri, Feb 29, 2008 at 2:06 AM, Dario Saccavino <kathoum@gmail.com> wrote:
> 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.

Ahh, that must have been the source of my confusion.  Thanks!

-jason

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-02-29 14:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-29  8:07 const issues Jason Pepas
2008-02-29  8:13 ` Dario Saccavino
2008-02-29 15:14   ` Jason Pepas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).