簡體   English   中英

來自 gcc 的警告:標量初始化器周圍的大括號,如何修復?

[英]Warning from gcc: braces around scalar initializer, how to fix?

在重寫 kernel 驅動程序時,我收到了以下警告:

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

請閱讀當我在 {} 中聲明一個結構的字段時出現此警告:

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

但我的結構在 {} 中有 2-3 個字段:

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,
    },
};

這種結構:

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);
};

我該如何解決這個警告? Qualcomm 內核正在使用 -Werror 標志構建,因此此警告至關重要。

static struct device_attribute *opalum_dev_attr表示將 opalum_dev_attr 聲明為 static 指向struct device_attribute的指針

您的代碼正在嘗試初始化 struct device_attribute的 static數組

你想要的是: static struct device_attribute opalum_dev_attr[]這意味着將 opalum_dev_attr 聲明為 static struct device_attribute 數組

這是因為您初始化指向結構的指針而不是結構本身。

您需要分配對結構的引用,例如使用復合文字

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}, };

} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM