Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 07 Oct 2011 01:51:40 -0500 Message-ID: <4E8EA17E.56DD@mindspring.com> Date: Fri, 07 Oct 2011 02:51:42 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: initialization of local static variable References: <16f1e150-05c8-4e30-a0e3-17aa3182f14d@d28g2000vby.googlegroups.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 60 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.204 X-Trace: sv3-EmoFjv6G8cGZrTvo+aTj4qHSSVtU88lvvU6XO5HqWNtSO8EFCP1HMQ5qkzJHqvFyJQIP6tTC/UsFLnK!wgZGKYY3siomANDllbSM3je0LMLFKmEsiT7ZllEKsJG/7pgjWauB9yB1WFEi7AlkbHh356Vmql0E!dIlyMepod50= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2812 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:11316 V.Subramanian, India wrote: > > consider the following program x.c: > #include > #include > > char first[] = "test message"; > > void display(void) > { > static char* p_first = first + 5; > > printf("%s\n", p_first); > > char second[] = "new message"; > static char* p_second = second + 4; > > printf("%s\n", p_second); > > return; > } > > int main() > { > display(); > > return EXIT_SUCCESS; > } > > When this program is compiled with > gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1) > as > gcc -std=c99 -pedantic -Wall -Wextra x.c > it generates the following error: > x.c: In function `display': > x.c:13: error: initializer element is not constant > > The compiler accepts the initialization of the local static variable > 'p_first' in 'display()' function. However the compiler reports error > for the initializtion of the local static variable 'p_second' in > 'display()' function. > > In the case of 'p_first', the array name 'first' decays into a pointer > to the first element and so in this case ALSO the memory address > stored in p_first will be known only at run-time(similar to that of > 'p_second'). So why doesn't the compiler flag this as an error ? > > Please clarify. ISO/IEC 9899 6.6 Constant expressions 9 An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type. -- pete