From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17481 invoked by alias); 29 Feb 2008 08:07:33 -0000 Received: (qmail 17428 invoked by uid 22791); 29 Feb 2008 08:07:32 -0000 X-Spam-Check-By: sourceware.org Received: from ti-out-0910.google.com (HELO ti-out-0910.google.com) (209.85.142.189) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 29 Feb 2008 08:07:05 +0000 Received: by ti-out-0910.google.com with SMTP id j3so3295880tid.20 for ; Fri, 29 Feb 2008 00:07:00 -0800 (PST) Received: by 10.151.144.4 with SMTP id w4mr3124559ybn.199.1204272419069; Fri, 29 Feb 2008 00:06:59 -0800 (PST) Received: by 10.150.12.17 with HTTP; Fri, 29 Feb 2008 00:06:59 -0800 (PST) Message-ID: Date: Fri, 29 Feb 2008 08:13:00 -0000 From: "Dario Saccavino" To: "Jason Pepas" Subject: Re: const issues Cc: gcc-help@gcc.gnu.org In-Reply-To: <56837de30802282311i2bec1b15te5a4dc6c019e27c9@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <56837de30802282311i2bec1b15te5a4dc6c019e27c9@mail.gmail.com> X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-02/txt/msg00369.txt.bz2 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 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 : > this compiles as expected: > > > #include > > 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 > > 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 >