Constant array types in C, flaw in standard?

前端 未结 3 1855
迷失自我
迷失自我 2021-01-04 02:21

Paragraph 6.7.3.8 of the C99 spec states

If the specification of an array type includes any type qualifiers, the element type is so-qualified, not the array ty
3条回答
  •  伪装坚强ぢ
    2021-01-04 03:04

    Possible workaround for the C programmer (but not the compiler designer):

    gcc with -Wcast-qual does not complain about this:

    void g(const double *ap, int n)
    {
        int i;
        struct box 
        {
          double a[n];
        };
        const struct box *s = (const struct box *)ap;
    
        for (i=0; ia[i]);
           /* ... */
        }
    }
    

    Even if it's not very elegant. The trailing array member a also has a slightly different meaning between C89 and C99, but at least you get the intended effect.

提交回复
热议问题