[Bug c/65106] C99 intialization of struct with const member through a non-const pointer

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Feb 19 15:00:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65106

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Function arguments and return values don't modify existing objects, the copies
are new objects. In the assignment in your code you modify an existing object.
If it was allowed what would prevent you from doing:

  struct A a = { 1, 2 }
  struct A *ptr = &a;
  *ptr = (struct A) {10, 10};

Or simply:

  struct A *ptr = malloc(sizeof(struct A));
  *ptr = (struct A) {10, 10};
  *ptr = (struct A) {11, 11};

(Clang allows both of these, despite the fact they modify a const object).



More information about the Gcc-bugs mailing list