包含嵌套struct的struct实例的静态初始化 [英] Static initialization of struct instance that contains nested struct

查看:95
本文介绍了包含嵌套struct的struct实例的静态初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们不能在c中创建静态结构,我们也不能在c中的结构中包含静态成员,但是我们可以创建struct static的实例,然后所有成员将被视为该实例的静态,

i想知道我的结构是否包含另一个结构,因为它的成员将使其实例静态也会使这些嵌套的结构变量成为静态。



we cannot make static struct in c, we also cannot have static members inside a struct in c, but we can make instance of struct static then all members will be treated as static for that instance,
i would like to know if my structure contains another struct as its member will making its instance static will also make those nested struct variables static.

#include <stdio.h>
#include <string.h>

struct eg {
struct nestedstruct nsvar*,
    int n;
};

void test() {
    static struct eg X = {0};
    printf("%d\n", X.n++);
}

int main() {
    int i;
    for (i=0;i<4;i++) test();

    return 0;
}

推荐答案

存储类说明符'static',当提到变量时,有2个属性:变量是在程序存在期间分配在BSS部分并保留其内容,第二个是它的范围仅限于声明实例,并且从不在声明它的模块之外(静态声明的变量不能使用'extern'限定符访问)。 br />
这意味着您总是从程序的开头到结尾占用该实例的内存。

您可以拥有与静态变量同名的实例。你想要的,因为他们在彼此的范围之外。如果它们具有不同的名称,您可以在任何范围内拥有任意数量的结构实例。

这意味着您始终占用程序开始到结束的所有实例的内存。 />
参见此示例:

The storage class specifier 'static', when refered to a variable, have 2 properties: the variable is allocated in BSS section and retain its contents during program existence, the second is that its scope is limited to the declaration instance and never outside the module where it is declared (a static declared variable cannot be accessed with the 'extern' qualifier).
This means that you always occupy the memory of that instance from the start to the end of the program.
You can have as many instances of static variables with the same name as you want while they are outside of the scope of each other. You can have any number of instancies of your structure at whichever scope if they have different names.
This means that you always occupy the memory of all instances from the start to the end of the program.
See this example:
typedef _tagMyStruct
{
    int a;
    struct _tag_Struct2
    {
        float c;
        int   d
    } MySecondStruct;
} MyExampleStruct;

//This instance is available in this whole module, but not extern modules, unless a
//variable with same name is declared in a local scope. 
static MyExampleStruct MyStruct;

int foo(void)
{
    static MyExampleStruct MyStruct;
    //Any reference to MyStruct in this function refers to local instance above
    //that is a static instance anyway ;-)
    ...
}

int bar(void)
{
    static MyExampleStruct MyStruct;
    //Any reference to MyStruct in this function refers to local instance above
    //that is a static instance anyway ;-)
    ...
}

int fun(void)
{
    //Any reference to MyStruct in this function refers to module instance
    //defined outside function that is a static instance anyway ;-)
    ...
}





结构声明不能声明为静态,但它的实例可以。

你不能在里面有静态成员结构,因为结构的成员继承了包含结构的存储类。因此,如果结构被声明为静态,则所有成员都是静态的,甚至包括子结构。但是如果你将指针包含在结构中(如你的样本中),那么子结构当然具有各自实例的存储类。



A structure declaration cannot be declared static, but its instancies can.
You cannot have static members inside a structure because the members of a structure inherist the storage class of the containing struct. So if a structure is declared to be static all members are static even included substructures. But if you include pointers to structures (as in your sample) of course the sub structures have the storage class of their respective instances.


是的,静态意味着程序中只有一个实例。为什么你不能更好地学习语言或尝试这些琐碎的东西。



提示:对于结构使用比eg更好的名称。使用大字母和解释名称(如PROGRAM_DATA)是常见的代码风格。
Yes, static means that there is only ONE instance in the program. Why dont you better learn the language or try such trivial stuff out.

Tip: Use better names than "eg" for structs. It is common code style to use big letters and an explaining name like PROGRAM_DATA.


这篇关于包含嵌套struct的struct实例的静态初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆