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)
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.