Re: [cc65] struct + const confusion?

From: Dirk Jagdmann <doj1cubic.org>
Date: 2009-10-08 00:00:44
> typedef struct {
>     char foo;
> } Bar;
> 
> void main() {
>     Bar bar;
>     Bar* const baz = &bar;
>     baz->foo = 1;
> }
> 
> This produces the compiler error "test.c(9): Error: Assignment to const"
> 
> Shouldn't be an error, should it? baz is const, bar isn't.

Actually only the pointer baz is const, not the pointed-to struct (well
according to C rules), so this looks like cc65 doesn't implement const handling
correctly.

The following two tests show how the const rules have to be enforced by the
compiler:

void main() {
  Bar bar;
  Bar * const baz = &bar;
  baz = 3; /* error, pointer to baz is const */

  Bar * bay const = &bar;
  bay = 4; /* same as above, since "const" relates to the left "thing" */
}

void main() {
  Bar bar;
  const Bar * baz = &bar;
  baz->foo = 3; /* error, baz is const */

  Bar const * bay = &bar;
  bay->foo = 4; /* same as above, since "const" relates to the left "thing" */
}

-- 
---> Dirk Jagdmann
----> http://cubic.org/~doj
-----> http://llg.cubic.org
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Thu Oct 8 00:01:40 2009

This archive was generated by hypermail 2.1.8 : 2009-10-08 00:01:42 CEST