C 编译器忽略 'static' 的结构声明

标签 c struct static

在 C 中,如果我像这样声明一个结构:

static struct thing {
    int number;
};

并编译它(在这种情况下使用 gcc),编译器打印出这个警告:

warning: 'static' ignored on this declaration

[-Wmissing-declarations]

这是为什么?

我将结构设为静态的目的是将 thing 保留在全局命名空间之外,以便另一个文件可以根据需要声明自己的 thing

最佳答案

您不能在不定义实际对象的情况下定义存储。

static struct thing {
    int number;
}obj1,obj2;

没问题,并且:

struct thing {
    int number;
};

static struct thing x,y;

关于C 编译器忽略 'static' 的结构声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52087025/

相关文章:

c - 不使用选择排序对三个指针进行排序

c - C 中的嵌套结构访问

C编程: Struct array construction while reading a file

当所有类实例超出范围时,Java 重置私有(private)静态成员

java - 如何正确调用方法

ios - 如何将此 C 库添加到我的 iOS Xcode 4 项目中?

在 C 中从年中日转换为月日

pointers - Go 中指向结构或数组值的指针如何工作?

c++ - 使用结构体从 .txt 文件读取着色器

c++ - C++ 语言定义对 static 关键字的范围有什么看法?