错误:标签只能是语句的一部分
我正在用 C 语言编写一个该死的解释器,但在使用我不习惯的东西时遇到了一些麻烦。在 Brainfuck 中,逗号 (,) 本质上是 getchar()。所以我有以下代码:
//This is just ptr
static char *ptr;
switch (command)
{
case ',':
*ptr=getchar(); // Here's the code causing error
break;
}
当我尝试编译它时,gcc throws error: a label can only be part of a statements and a statements not astate
。
有什么想法吗? (抱歉,不太熟悉这个错误)
I'm writing a brainfuck interpreter in C, and I'm having a little bit of trouble with the use of somethings I'm not used to. In brainfuck, a comma ( ,) is essentially getchar(). So I have the following code:
//This is just ptr
static char *ptr;
switch (command)
{
case ',':
*ptr=getchar(); // Here's the code causing error
break;
}
gcc throws error: a label can only be part of a statement and a declaration is not a statement
at me when I try to compile this.
Any ideas? (Sorry about this, not so familiar with this error)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信你的意思
是因为
*=
意味着将左侧的值与右侧的值相乘,并将其分配给左侧的值。但是,您希望取消引用ptr
并将getchar
的结果写入该位置。除此之外,您的代码可以与我的 gcc 版本完美地编译(如果我在某处声明
command
),因此您显然没有向我们展示完整的示例。I believe you mean
instead of
Because
*=
means multiply the value on the left side with the value on the right side and assign this to the left value. However, you want to dereferenceptr
and write the result ofgetchar
to that location.Other than that your code compiles perfectly fine with my version of gcc (if I declare
command
somewhere), so you are obviously not showing us a complete example.这完全是我的错误,我之前已经注释掉了之前的代码。我认为这是导致错误的代码,因为我同时注释掉了两个代码,并且它不会导致此错误。不过,我尝试将两者都注释掉,现在我明白了原因。
这是关于 FILE 以及在不同情况下使用我的搜索的问题。
This was my mistake entirely, I had previously commented out the code before it. I thought that this was the code causing the error due to me commenting out both codes at the same time, and it not causing this error. However I tried to comment out both, and now I understand why.
It was something about FILE and using my seek in a different case.