This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: "gcc" complains about a constant being non constant...(sorry for dup)
- From: Andrew Haley <aph at redhat dot com>
- To: "Linda A. Walsh" <gcc at tlinx dot org>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 06 Jul 2009 10:47:22 +0100
- Subject: Re: "gcc" complains about a constant being non constant...(sorry for dup)
- References: <4A518DD9.5090400@tlinx.org>
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 <stdio.h>
> 2 #include <stdlib.h>
> 3 #include <strings.h>
> 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.