why error C2051?

  • Follow


const int c=9;

void main()
{
	int a=9;
	switch (a)
	{
	case c:
		a=c+1;
		break;
	}
}
-----------
compile result:
c.c(8) : error C2051: case expression not constant
-----------
But [c] is a const int....why?

0
Reply zhangchunlin (3) 11/17/2006 7:21:47 AM

clino said:

> const int c=9;
> 
> void main()

int main(void)

> {
> int a=9;
> switch (a)
> {
> case c:
> a=c+1;
> break;
> }
> }
> -----------
> compile result:
> c.c(8) : error C2051: case expression not constant
> -----------
> But [c] is a const int....why?

It's a const int all right, but it isn't a constant integer expression, 
which is what you need in a case expression.

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not 
adjust your email clients.
0
Reply invalid171 (6558) 11/17/2006 7:54:59 AM


"clino" <zhangchunlin@gmail.com> wrote:

> const int c=9;
> 
> void main()

<http://c-faq.com/ansi/maindecl.html>
<http://c-faq.com/ansi/voidmain.html>
<http://c-faq.com/ansi/voidmain3.html>
<http://c-faq.com/ansi/voidmainbooks.html>

> {
> 	int a=9;
> 	switch (a)
> 	{
> 	case c:
> 		a=c+1;
> 		break;
> 	}
> }
> -----------
> compile result:
> c.c(8) : error C2051: case expression not constant
> -----------
> But [c] is a const int....why?

<http://c-faq.com/cpp/constvsdefine.html>
<http://c-faq.com/ansi/constasconst.html>

HTH; HAND; RTFFAQ.

Richard
0
Reply rlb (4118) 11/17/2006 8:08:16 AM

very clearly~
Thank u very much~

On 11=D4=C217=C8=D5, =CF=C2=CE=E74=CA=B108=B7=D6, r...@hoekstra-uitgeverij.=
nl (Richard
Bos) wrote:
> "clino" <zhangchun...@gmail.com> wrote:
> > const int c=3D9;
>
> > void main()<http://c-faq.com/ansi/maindecl.html>
> <http://c-faq.com/ansi/voidmain.html>
> <http://c-faq.com/ansi/voidmain3.html>
> <http://c-faq.com/ansi/voidmainbooks.html>
>
> > {
> >    int a=3D9;
> >    switch (a)
> >    {
> >    case c:
> >            a=3Dc+1;
> >            break;
> >    }
> > }
> > -----------
> > compile result:
> > c.c(8) : error C2051: case expression not constant
> > -----------
> > But [c] is a const int....why?<http://c-faq.com/cpp/constvsdefine.html>
> <http://c-faq.com/ansi/constasconst.html>
>=20
> HTH; HAND; RTFFAQ.
>=20
> Richard

0
Reply zhangchunlin (3) 11/17/2006 8:34:01 AM

3 Replies
41 Views

(page loaded in 0.307 seconds)


Reply: