使用keyfromString函数切换字符串的大小写

时间:2018-03-30 12:25:19

标签: c

我已使用此代码将switch case用于字符串

#define BADKEY -1
#define b 1
#define s 2
#define r 3
#define p 4

typedef struct { char *key; int val; } t_symstruct;

static t_symstruct lookuptable[] = {
{"b",b},{"s",s},{"r",r,{"p",p}
 };

#define NKEYS (sizeof(lookuptable)/sizeof(t_symstruct))

int main () {

这里有什么错误?

 int keyfromstring(char *key)
 {
      int i;
      for (i=0; i < NKEYS; i++) {
         if (strcmp(lookuptable[i].key, key) == 0)
            return lookuptable[i].val;
         }
    return BADKEY;
 }

切换案例

switch (keyfromstring(field1))
{
     case b :
     printf(" is b\n" );
     break;

     case s :
     printf("is s\n" );
     break;

     case r :
     printf(" is r\n" );
     break;

     case ps:
     printf("is p\n" );
     break;

     case BADKEY:
     printf("Case: BADKEY \n");
     break;

     default://case if nothing
     printf("Case: nothing happen\n"); 
}

**我收到此消息错误分段错误(核心转储) 正确打印一些行后,请问这里的错误是什么?

2 个答案:

答案 0 :(得分:0)

在传递给函数之前检查field1可能存在一些问题。我没有看到任何错误。再加上你没有明确提到什么时候会崩溃

如果在打印开关案例结束时遇到错误分段错误(核心转储)一次。你可能会在结束时变为空 检查feild1是否为空。

答案 1 :(得分:0)

lookuptable有拼写错误,它应该是:

static t_symstruct lookuptable[] = {
    {"b",b}, {"s",s}, {"r",r},  {"p",p}
};

您的原始版本甚至无法编译。测试程序如下:

#include <stdio.h>
#include <string.h>

#define BADKEY -1
#define b 1
#define s 2
#define r 3
#define p 4

typedef struct {
    char *key;
    int val;
} t_symstruct;

static t_symstruct lookuptable[] = {
    {"b",b}, {"s",s}, {"r",r},  {"p",p}
};

#define NKEYS (sizeof(lookuptable)/sizeof(t_symstruct))

int keyfromstring(char *key)
{
    int i;
    for (i=0; i < NKEYS; i++) {
        if (strcmp(lookuptable[i].key, key) == 0)
            return lookuptable[i].val;
    }
    return BADKEY;
}

int main (void) {

    char * arr[] = { "b","s","r", "p", "!", NULL} ;
    int i;

    for (i =0; arr[i] !=NULL; i++)
    {   
        switch (keyfromstring(arr[i]))
        {
        case b :
            printf("%s is b\n", arr[i] );
            break;

        case s :
            printf("%s is s\n", arr[i] );
            break;

        case r :
            printf("%s is r\n", arr[i] );
            break;

        case p:
            printf("%s is p\n", arr[i] );
            break;

        case BADKEY:
            printf("Case: BADKEY: %s \n",  arr[i] );
            break;

        default://case if nothing
            printf("Case: nothing happen\n");
            break;
        }

    }

    return 0;
}

测试:

b is b                                                                                                                                           
s is s                                                                                                                                           
r is r                                                                                                                                           
p is p                                                                                                                                           
Case: BADKEY: !