initializer element is not constant - how to solve

2019-08-28 23:19发布

问题:

following this link

I did:

static const struct attribute const *attrs1= {
        &foo_attribute.attr,
        NULL,
};
 static const struct attribute_group const attr_group = {
       .attrs = attrs1,
};

but get these errors:

error: initializer element is not constant
error: (near initialization for 'attr_group.attrs')

found this solution but didn't understand how to solve it...

EDIT: the line triggered the error:

.attrs = attrs1,

回答1:

Yes, another struct object or the contents of another variable will never be considered a constant expression that could be used in an initializer for a static object.

But your first initialization also is bogus. Probably you meant

static const struct attribute * const attrs1= &foo_attribute.attr;

So your initialization of the second would read something like

static const struct attribute_group attr_group = {
       .attrs = &foo_attribute.attr,
};