错误:标签只能是语句的一部分

发布于 2024-12-22 00:41:32 字数 402 浏览 3 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-12-29 00:41:32

我相信你的意思

*ptr = getchar();

是因为

ptr*=getchar();

*= 意味着将左侧的值与右侧的值相乘,并将其分配给左侧的值。但是,您希望取消引用 ptr 并将 getchar 的结果写入该位置。


除此之外,您的代码可以与我的 gcc 版本完美地编译(如果我在某处声明 command ),因此您显然没有向我们展示完整的示例。

I believe you mean

*ptr = getchar();

instead of

ptr*=getchar();

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 dereference ptr and write the result of getchar 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.

妖妓 2024-12-29 00:41:32

这完全是我的错误,我之前已经注释掉了之前的代码。我认为这是导致错误的代码,因为我同时注释掉了两个代码,并且它不会导致此错误。不过,我尝试将两者都注释掉,现在我明白了原因。

这是关于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文