奇怪的类型错误

时间:2013-03-13 21:12:23

标签: 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:错误:'字母'未声明(首次使用此功能)”

1 个答案:

答案 0 :(得分:2)

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

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

编辑:原因很简单,标签只能跟一个声明,声明或初始化不是一个声明,而一个块(即复合声明)是。