Assign to const pointer in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Baldrian

    #1

    Assign to const pointer in C

    In an older version of gcc, I could cast away const.
    When porting to gcc 4.1.2, the compiler complains.
    Code:
    char * const buffer;
    ...
       (char*)buffer = malloc(SIZE);
    In C++, there is a const_cast<>, but how should this be done in C?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Well, does that make sense to you? You define the variable as a constant, meaning it is immutable (cannot be changed). If you're wanting to modify the data of this constant, instead define it without 'const'. Makes sense to me.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Is the gcc complaint a warning or an error? My recollection is that it is a warning. If so, then you don't have to change anything -- just live with it.

      Comment

      Working...