0

At rewriting kernel driver I got this warning:

msm-cirrus-playback.c:545:2: warning: braces around scalar initializer

Read that this warning appears when I am declaring one structure's field in {}:

struct random_struct test = {
    { .name = "StackOverflow" },
    { .name = "StackExchange" },
};

But my structure have 2-3 fields in {}:

static struct device_attribute *opalum_dev_attr = {
    {
    .attr->name = "temp-acc",
    .show = opsl_temp_acc_show,
    .store = opsl_temp_acc_store,
    },
    {
    .attr->name = "count",
    .show = opsl_count_show,
    .store = opsl_count_store,
    },
    {
    .attr->name = "ambient",
    .show = opsl_ambient_show,
    .store = opsl_ambient_store,
    },
    {
    .attr->name = "f0",
    .show = opsl_f0_show,
    .store = opsl_f0_store,
    },
    {
    .attr->name = "pass",
    .show = opsl_pass_show,
    },
    {
    .attr->name = "start",
    .show = opsl_cali_start,
    },
};

This structure:

struct device_attribute {
    struct attribute    attr;
    ssize_t (*show)(struct device *dev, struct device_attribute *attr,
            char *buf);
     ssize_t (*store)(struct device *dev, struct device_attribute *attr,
             const char *buf, size_t count);
};

How can I fix this warning? Qualcomm kernels are building with -Werror flag, so this warning is critical.

tdrkDev
  • 69
  • 4
  • Does this answer your question? [gcc warning: braces around scalar initializer](https://stackoverflow.com/questions/3462513/gcc-warning-braces-around-scalar-initializer) – SiegeX May 31 '20 at 19:52
  • @SiegeX Nope, he had different reason – tdrkDev May 31 '20 at 19:57
  • `.attr->name =` is not valid syntax for a designation in an initializer. Perhaps you meant `.attr.name =`? – Ian Abbott Jun 01 '20 at 11:29

2 Answers2

2

static struct device_attribute *opalum_dev_attr means declare opalum_dev_attr as static pointer to struct device_attribute

Your code is trying to initialize a static array of struct device_attribute

What you want is: static struct device_attribute opalum_dev_attr[] which means declare opalum_dev_attr as static array of struct device_attribute

SiegeX
  • 135,741
  • 24
  • 144
  • 154
0

It is because you initialize the pointer to the struct not the struct itself.

you need to assign the reference to the struct for example by using compound literals

struct x 
{
    int a,b,c;
}a = {1,2,3};



void foo()
{
    struct x a = {1,2,3};
    struct x *b = {1,2,3}; // wrong warning here
    struct x *x = &(struct x){1,2,3}; // correct reference to the struct assigned (using compound literal
    struct x *y = (struct x[]){{1,2,3}, {4,5,6}, {4,5,6}, };
    struct x z[] = {{1,2,3}, {4,5,6}, {4,5,6}, };

} 
0___________
  • 60,014
  • 4
  • 34
  • 74