5

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)

Joshua Hedges
  • 255
  • 1
  • 3
  • 13

2 Answers2

7

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.

bitmask
  • 32,434
  • 14
  • 99
  • 159
1

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.

Joshua Hedges
  • 255
  • 1
  • 3
  • 13
  • If I could have a nickel for every time I encountered a C syntax error wrapped like a christmas present inside of another syntax error, I would be a very wealthy man. C has its up, and its downs such as this. – Jack G Jan 13 '18 at 03:09