I'm getting initialization discards ‘const’ qualifier from pointer target type
warning in the line .grid_col = &c_ax_gd
i.e. assigning an address expression to a pointer, which is part of a constant structure.
struct color {
double r;
double g;
double b;
double a;
};
struct graph {
double origin[2];
double dim[2];
double scale[2];
double grid_line_width;
double axes_line_width;
struct color *grid_col;
struct color *axes_col;
};
int main(void) {
static const struct color c_ax_gd = {0.5, 0.5, 0.5, 1};
static const double h = 600, w = 800;
const struct graph g = {
.origin = {w / 2, h / 2},
.dim = {w, h},
.scale = {w / 20, h / 20},
.grid_line_width = 0.5,
.axes_line_width = 1,
.grid_col = &c_ax_gd,
.axes_col = &c_ax_gd
};
....
}
I found the following in C99 standard
More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one of the following:
- an arithmetic constant expression,
- a null pointer constant,
- an address constant, or
- an address constant for an object type plus or minus an integer constant expression.
An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type
My question is, doesn't that mean &c_ax_gd
is an address constant? If so, how does using an address constant inside an initializer for a constant structure discards the const
qualifier?