c - 奇怪的类型转换错误

标签 c serialization casting

所以,我有这个功能,但我遇到了一些我无法弄清楚的非常奇怪的错误。

void serialize_helper(huff *h, bits *history, char** a)
{
  switch (h->tag) {
  case LEAF:
    char letter = h->h.leaf.c;
    int arraynum = (int)letter;
    a[arraynum] = bits_show(history);
    putchar('\n');
    return;
  case NODE:
    /* traverse left subtree */
    bits_putlast('0',history);
    serialize_helper(h->h.node.lsub,history, a);
    bits_remove_last(history);
    /* traverse right subtree */
    bits_putlast('1',history);
    serialize_helper(h->h.node.rsub,history, a);
    bits_remove_last(history);
    return;
  default:
    fprintf(stderr,"main.serialize_helper: bad tag\n");
    exit(1);
  }
}

我收到一个简单变量定义的错误(来自 char letter = ...;):

“huffenc.c:18:错误:‘char’之前的预期表达式”

此外,编译器的行为就像我对“字母”的声明不存在一样: “huffenc.c:19: error: ‘letter’ undeclared (first use in this function)”

最佳答案

如果您想在 case 之后直接在 switch 中定义变量,您需要有一个 block ,例如

  case LEAF: {
    char letter = h->h.leaf.c;
    int arraynum = (int)letter;
    a[arraynum] = bits_show(history);
    putchar('\n');
    return;
  }

编辑:原因很简单,标签只能跟在语句之后,声明或初始化不是语句,而 block (即复合语句)是。

关于c - 奇怪的类型转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396570/

相关文章:

c - 如何链接到不同的 libc 文件?

c - 如何在不修改程序的情况下通过fork启动一个进程的两个实例

java - java中通过序列化获取多个对象

json - 序列化和反序列化 pandas periodIndex 系列

java - 动态(运行时)多态性在java中如何工作?换句话说,JVM 如何知道要调用哪些方法?

c# - 基类数组到派生类数组的转换

c - 是否可以实时监控linux下的所有系统调用?

c - C语言中的数组

scala - Scala/Akka 中的序列化

C++ 将 bool 转换为 int - 标准