您如何在C中定义常量? [英] How do you define a constant in C?
本文介绍了您如何在C中定义常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试构建一个开关,但显示表达式必须具有整数或枚举类型。我猜想winapi中的开关不会接受LPSTR类型的变量?
I'm trying to build a switch and I get an "expression must have integral or enum type". I'm guessing the switch in winapi doesn't take variables with LPSTR type?
char testbuf[ 51 ]; // allocate space for 50 characters + termination
LPSTR test = testbuf;
scanf("%50s", test); // read at most 50 characters
printf("%s", test);
switch ( test ) {
case "etc" :
{
}
break;
推荐答案
我通常使用一种字典模式:
I usually use a kind of dictionary pattern:
enum {
keyETC,
keyOther,
keyUnknown
};
struct SDictionary{
int key;
char *name;
} dict[] = {
{ keyETC, "etc" },
{ keyOther, "bla" },
{ 0 }
};
// lookup
struct SDictionary *pCandidate = dict;
int key = keyUnknown;
while (pCandidate->name)
{
if (0 == strcmp(test, pCandidate->name)
{
key = pCandidate->key;
break;
}
}
switch (key)
{
case keyETC:
// ....
}
这篇关于您如何在C中定义常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文