From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24201 invoked by alias); 6 Jul 2009 09:47:36 -0000 Received: (qmail 24193 invoked by uid 22791); 6 Jul 2009 09:47:35 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx2.redhat.com (HELO mx2.redhat.com) (66.187.237.31) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 06 Jul 2009 09:47:27 +0000 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n669lP2h004126; Mon, 6 Jul 2009 05:47:25 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n669lO3l020615; Mon, 6 Jul 2009 05:47:24 -0400 Received: from zebedee.pink (vpn-12-145.rdu.redhat.com [10.11.12.145]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n669lN2u016253; Mon, 6 Jul 2009 05:47:23 -0400 Message-ID: <4A51C82A.9030302@redhat.com> Date: Mon, 06 Jul 2009 09:47:00 -0000 From: Andrew Haley User-Agent: Thunderbird 2.0.0.17 (X11/20081009) MIME-Version: 1.0 To: "Linda A. Walsh" CC: gcc-help@gcc.gnu.org Subject: Re: "gcc" complains about a constant being non constant...(sorry for dup) References: <4A518DD9.5090400@tlinx.org> In-Reply-To: <4A518DD9.5090400@tlinx.org> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 2009-07/txt/msg00083.txt.bz2 Linda A. Walsh wrote: > (Sorry if this is a dup, somehow my from address got mangled with the from > addr having this message being sent from my system's MAILER DAEMON! > Weird.) > > I have a "proglet", included below (twice, in fact! :-), first with line > numbering for referring to the error messages, and a 2nd time without > line numbers to allow for easy cut & pasting to try it in your local > environment. > > The error output appears to indicate a problem with compile-time constant > folding. > > gcc --version shows: > gcc (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291] > > Compile time output: > ct.c:10: error: initializer element is not constant > ct.c:11: error: initializer element is not constant > > Here's the proglet w/line numbering for reference: > ------ > 1 #include > 2 #include > 3 #include > 4 5 typedef const char * String; > 6 7 static const String days [] ={"Sun", "Mon", "Tue", "Wed", "Thu" , > "Fri", "Sat"}; > 8 static const int sizeof_days = sizeof(days); > 9 static const int sizeof_String = sizeof(String); > 10 static const int numdays = sizeof_days / sizeof_String; > 11 static const int last_index=numdays-1; > 12 13 int main (){ > 14 printf("numdays=%d, lastidx=%d\n",numdays,last_index); > 15 } > -------- > > if, in line 8, sizeof(days) is a constant, AND in line 9, > sizeof(String) is > a constant, then how can their division (in line 10) NOT be a constant? Simple: sizeof(days) is a constant, but sizeof_days is not. Reason: because the C language standard says so. A const variable is not a constant. It'd be nice if it were. Andrew.