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

andrii.riabushenko at barclays dot com gcc-bugzilla@gcc.gnu.org
Wed Feb 18 13:28:00 GMT 2015


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

            Bug ID: 65106
           Summary: C99 intialization of struct with const member through
                    a non-const pointer
           Product: gcc
           Version: 4.9.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrii.riabushenko at barclays dot com

I noticed that such C99 code is valid in Clang, but not in GCC:

#include<stdlib.h>

struct A {
  int x;
  const int y;
}

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

I find this language construct rather useful. I quickly scanned the C standard
and I can't find the evidence that is actually prohibited. There is a
difference between assignment a const member (ptr->y = 10) that should be
prohibited and actual struct initialization through non-const pointer. For some
reason GCC does not distinguish these 2 cases.



More information about the Gcc-bugs mailing list