所以,我有这个功能,但我遇到了一些我无法弄清楚的非常奇怪的错误。
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/