error: A label can only be part of a statement

Joshua Hedges picture Joshua Hedges · Dec 19, 2011 · Viewed 31.4k times · Source

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)

Answer

bitmask picture bitmask · Dec 19, 2011

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.