【问题标题】:Designated Initializers: GCC warning message: near initialization and other warning messages指定初始化器:GCC 警告消息:接近初始化和其他警告消息
【发布时间】:2014-03-30 15:23:04
【问题描述】:

我正在尝试在 linux 内核中了解更多关于 kobject 的信息,并且在尝试编写使用此类工具的模块时,我收到了错误和警告消息,因此我在此处放置了相关数据的精简版本结构和对应的gcc的错误和警告信息。

$ gcc issue.c 
issue.c:30:1: error: initializer element is not constant
 } ;
 ^
issue.c:30:1: error: (near initialization for ‘first.attr’)
issue.c:34:1: error: initializer element is not constant
 }; 
 ^
issue.c:34:1: error: (near initialization for ‘second.attr’)
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default]
 struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs };
        ^
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[0]’) [enabled by default]
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default]
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[1]’) [enabled by default]

以及示例代码:

#include <stdio.h>

struct attribute {
    const char      *name;
    unsigned short          mode;
};

struct bin_attribute {
    struct attribute    attr;
    unsigned int        size;
    void            *private;
};

struct attribute_group {
    const char      *name;
    struct attribute    **attrs;
    struct bin_attribute    **bin_attrs;
};

struct attribute first_attr = {
    .name = "FIRST"
}; 

struct attribute second_attr = {
    .name = "SECOND"
};

struct bin_attribute first = {
    .attr = first_attr
} ;

struct bin_attribute second = {
    .attr = second_attr
}; 

struct bin_attribute *first_bin_attrs = &first;
struct bin_attribute *second_bin_attrs = &second;

struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs };

int main(void)
{
    struct attribute_group my_group = {
        .name = "xyz",
        .bin_attrs = my_bin_attrs,
    };

    return 0;
}

【问题讨论】:

  • 当您收到错误消息时,您应该 !!ALWAYS!! 在向Stack Overflow 提出相关问题之前,您应该在 Google 上尝试找出错误消息.
  • “和其他警告信息”对于Stack Overflow 的问题来说肯定不太理想。一个问题应该是关于一个错误(/问题)(它们是错误,而不是警告 - 有区别)。 Stack Overflow 上的帖子旨在为许多用户提供长期价值。如果您通过询问许多事情来污染帖子,那么任何人在寻找特定帖子时遇到此帖子的机会都很渺茫,如果他们这样做了,他们将不得不筛选所有其他不适用的相关信息到其他错误。

标签: c arrays gcc data-structures


【解决方案1】:

您不能将变量的值用作文件范围初始化程序中的初始化程序。

你有:

struct attribute first_attr = {
    .name = "FIRST"
}; 

…

struct bin_attribute first = {
    .attr = first_attr
};

第二个是生成“非常量初始化程序”错误,因为它试图使用不是编译时常量的表达式first_attr。你可能会在 C++ 中侥幸逃脱;你不能在 C 中。

你可以使用变量的地址;这算作编译时常量(在链接时解析),但不是变量的值。在函数内部,你也可以。

要么重新设计 struct bin_attribute,使 attr 成员是一个指针,要么放弃你显示的初始化。

第二组错误是由于:

struct bin_attribute *first_bin_attrs = &first;
struct bin_attribute *second_bin_attrs = &second;

struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs };

&amp;first_bin_attrs 的类型是struct bin_attribute **,所以初始化值中的指针层次太多了。如果您删除&amp;,则类型正确,但您又回到了“非常量初始化程序”问题。如果您将* 添加到my_bin_attrs 的类型中,您就有变成Three Star Programmer 的危险,您将不得不修改使用my_bin_attrs 的代码。

简单的解决方法是:

struct bin_attribute *first_bin_attrs = &first;
struct bin_attribute *second_bin_attrs = &second;

struct bin_attribute *my_bin_attrs[] = { &first, &second };

但这是否令人满意取决于您要达到的目标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多