c - 错误 : A label can only be part of a statement

标签 c pointers gcc compiler-errors getchar

<分区>

我正在用 C 写一个脑残的解释器,我在使用一些我不习惯的东西时遇到了一些麻烦。在 brainfuck 中,逗号 (,) 本质上是 getchar()。所以我有以下代码:

//This is just ptr
static char *ptr;

switch (command)
{
  case ',':
    *ptr=getchar(); // Here's the code causing error
    break;
}

当我尝试编译它时,gcc 向我抛出 error: a label can only be part of a statement and a declaration is not a statement

有什么想法吗? (抱歉,对这个错误不是很熟悉)

最佳答案

我相信你的意思

*ptr = getchar();

代替

ptr*=getchar();

因为*=的意思是把左边的值乘以右边的值,然后赋值给左边的值。但是,您想要取消引用 ptr 并将getchar 的结果写入该位置。


除此之外,您的代码在我的 gcc 版本中编译得很好(如果我在某处声明 command),所以您显然没有向我们展示完整的示例。

关于c - 错误 : A label can only be part of a statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8560734/

相关文章:

c++ - 使用条件语句在运行时选择不同的模板化矩阵类

c - 在线程中安全地返回浮点值

c++ - Windows中的管道句柄如何关闭? (_pclose() 的 fclose())?

c++ - 如何在不使用循环的情况下快速打印由指向指针的指针组成的二维字符数组?

linux - native GCC 5 二进制文件太大 (libexec)

gcc - 使用gcc 4.6在内核3.0上编译LLVM 2.9的gcc 4.2

c - 初始化指向 const 字符串的指针数组结构

c - 如何使用结构中的 union 访问 C 结构

c - 如何在C中调用具有void参数的Void函数

c - 指针类型的使用