如何在开关盒中使用const指针? [英] How to use a const pointer in switch case ?

查看:73
本文介绍了如何在开关盒中使用const指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在头文件中定义如下:

#define GLUT_BITMAP_9_BY_15((void *)2)

#define GLUT_BITMAP_8_BY_13((void *)3)

#define GLUT_BITMAP_TIMES_ROMAN_10((void *)4)

#define GLUT_BITMAP_TIMES_ROMAN_24((void *)5)


然后我在开关中使用constan,例如:

switch(font){

case GLUT_BITMAP_8_BY_13:bitmapHeight = 13;

休息;

case GLUT_BITMAP_9_BY_15:bitmapHeight = 15;

break;

case GLUT_BITMAP_TIMES_ROMAN_10:bitmapHeight

= 10;休息;

案例GLUT_BITMAP_TIMES_ROMAN_24:bitmapHeight

= 24;休息;

案例GLUT_BITMAP_HELVETICA_10:bitmapHeight =

10;休息;

案例GLUT_BITMAP_HELVETICA_12:bitmapHeight =

12;休息;

案例GLUT_BITMAP_HELVETICA_18:bitmapHeight =

18;休息;

}

然后我用gcc编译它们,gcc总是抱怨:


a8.c:261:错误:不允许指针作为案例值

a8.c:261:错误:案例标签不会减少为整数常量

a8.c:262:错误:指针不允许作为案例值

a8.c:262:错误:案例标签不会减少到整数常量


我无法转换GLUT_BITMAP_9_BY_15到一个整数

常数,并且不能抱怨传递,如何解决?


我知道我可以使用if ... elseif ...声明,真的我不能用

切换这种情况? switch只能使用整数常量?我告诉他们读取C编程语言,它就是这样说的。


由于什么原因,头文件将const定义为

(无效*)?


任何人都可以给我答案吗?


谢谢。



I have define in a head file like this:
#define GLUT_BITMAP_9_BY_15 ((void*)2)
#define GLUT_BITMAP_8_BY_13 ((void*)3)
#define GLUT_BITMAP_TIMES_ROMAN_10 ((void*)4)
#define GLUT_BITMAP_TIMES_ROMAN_24 ((void*)5)

then I use the constan in switch, like:
switch(font) {
case GLUT_BITMAP_8_BY_13: bitmapHeight = 13;
break;
case GLUT_BITMAP_9_BY_15: bitmapHeight = 15;
break;
case GLUT_BITMAP_TIMES_ROMAN_10: bitmapHeight
= 10; break;
case GLUT_BITMAP_TIMES_ROMAN_24: bitmapHeight
= 24; break;
case GLUT_BITMAP_HELVETICA_10: bitmapHeight =
10; break;
case GLUT_BITMAP_HELVETICA_12: bitmapHeight =
12; break;
case GLUT_BITMAP_HELVETICA_18: bitmapHeight =
18; break;
}
Then I use gcc to compile them, the gcc always complain:

a8.c:261: error: pointers are not permitted as case values
a8.c:261: error: case label does not reduce to an integer constant
a8.c:262: error: pointers are not permitted as case values
a8.c:262: error: case label does not reduce to an integer constant

And I can not convert the GLUT_BITMAP_9_BY_15 to a integer
constant, and can not complain pass, how to solve it?

I know I can use if ... elseif ... statement, really I can not use
switch in this case? switch can just only use a integer constant? I
read the "the C programming language", it said so.

And for what reason the header file will define the const to a
(void *)?

any one can give me a answer?

thanks.

推荐答案

qu **** ***@mail.whut.edu.cn 说:




我已定义在这样的头文件中:

#define GLUT_BITMAP_9_BY_15((void *)2)

#define GLUT_BITMAP_8_BY_13((void *)3)

#define GLUT_BITMAP_TIMES_ROMAN_10((void *)4)

#define GLUT_BITMAP_TIMES_ROMAN_24((void *)5)


I have define in a head file like this:
#define GLUT_BITMAP_9_BY_15 ((void*)2)
#define GLUT_BITMAP_8_BY_13 ((void*)3)
#define GLUT_BITMAP_TIMES_ROMAN_10 ((void*)4)
#define GLUT_BITMAP_TIMES_ROMAN_24 ((void*)5)



为什么?这是一个愚蠢的事情。

Why? That''s a daft thing to do.


然后我在开关中使用constan,如:

switch(font){

case GLUT_BITMAP_8_BY_13:bitmapHeight = 13;
then I use the constan in switch, like:
switch(font) {
case GLUT_BITMAP_8_BY_13: bitmapHeight = 13;



不能这样做。案例值应该是常量整数

表达式。

Can''t do that. Case values are supposed to be constant integer
expressions.


然后我使用gcc编译它们,gcc总是抱怨:


a8.c:261:错误:指针不允许作为案例值
Then I use gcc to compile them, the gcc always complain:

a8.c:261: error: pointers are not permitted as case values



正确。

Right.


a8.c:261:错误:案例标签不会减少到整数常量
a8.c:261: error: case label does not reduce to an integer constant



再次正确。

Right again.


我无法将GLUT_BITMAP_9_BY_15转换为整数

常量,并且不能抱怨传递,如何解决?
And I can not convert the GLUT_BITMAP_9_BY_15 to a integer
constant, and can not complain pass, how to solve it?



使用整数常量而不是指针。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Use integer constants instead of pointers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


On 3 ??27è?,????5ê ±47·?, Richard Heathfield< r ... @ see.sig.invalidwrote:
On 3??27è?, ????5ê±47·?, Richard Heathfield <r...@see.sig.invalidwrote:

quakew ... @ mail.whut.edu.cn说:
quakew...@mail.whut.edu.cn said:

hi,


我在头文件中定义如下:

#define GLUT_BITMAP_9_BY_15((void *)2)

#define GLUT_BITMAP_8_BY_13((void *)3)

#define GLUT_BITMAP_TIMES_ROMAN_10((void *) 4)

#define GLUT_BITMAP_TIMES_ROMAN_24((void *)5)
I have define in a head file like this:
#define GLUT_BITMAP_9_BY_15 ((void*)2)
#define GLUT_BITMAP_8_BY_13 ((void*)3)
#define GLUT_BITMAP_TIMES_ROMAN_10 ((void*)4)
#define GLUT_BITMAP_TIMES_ROMAN_24 ((void*)5)



为什么?这是一个愚蠢的事情。


Why? That''s a daft thing to do.



我不想那样使用,但那是过剩的。

(过剩是一个迷你包opengl。)

I do not want to use like that, but that is the glut''s head.
(glut is a mini package for opengl.)


qu ****** *@mail.whut.edu.cn 写道:

hi,


我在像这样的头文件:

#define GLUT_BITMAP_9_BY_15((void *)2)

#define GLUT_BITMAP_8_BY_13((void *)3)

#定义GLUT_BITMAP_TIMES_ROMAN_10((void *)4)

#define GLUT_BITMAP_TIMES_ROMAN_24((void *)5)


由于什么原因,头文件将定义const到一个

(无效*)?


I have define in a head file like this:
#define GLUT_BITMAP_9_BY_15 ((void*)2)
#define GLUT_BITMAP_8_BY_13 ((void*)3)
#define GLUT_BITMAP_TIMES_ROMAN_10 ((void*)4)
#define GLUT_BITMAP_TIMES_ROMAN_24 ((void*)5)

And for what reason the header file will define the const to a
(void *)?



我唯一能想到的是对抗用户!这是多么有悖常金的事情。


-

Ian Collins。

The only one I can think of is to antagonise users! What a perverse
thing to do.

--
Ian Collins.


这篇关于如何在开关盒中使用const指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆