C中的嵌套静态结构初始化 [英] Nested Static structure initialization in C

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

问题描述

我有一个嵌套结构,其中包含其他结构,目前当我从一个页面移动到另一个页面时,我之前初始化的结构丢失了它的值。通过使其静态我不确定我的结构的所有变量(甚至那些嵌套的结构变量)是否保持状态,我的结构的非结构变量在返回同一页面时确实保持其值(相同。 c文件)初始化它们。



例如:

i have a nested structure which contains others structure within it, currently when i move from one page to another my previously initialized structure loses its values. by making it static i am not sure whether all the variables of my structures (even those nested structure variables) maintain there state or not, the non-structure variable of my structure do maintain their value when coming back to the same page(same .c file) where they were initialized.

eg:

struct first {
    struct nestedfirst *nf;
    unsigned count;
    };







struct nestedfirst  {
    unsigned num;
    };





const struct first * f;





如果我将其声明为



static struct first * f;



nestedfirst结构值将在不同的文件(.c)调用之间保持可用,比如我在file1.c初始化它并移动到file2.c然后回到file1.cs我会发现它的值不变,如果不是我该怎么做才能使它发生。



const struct first *f;


if i declare it as

static struct first *f;

will the nestedfirst structure value will remain available between different file(.c) calls, say i initialized it at file1.c and move to file2.c then comeback to file1.cs will i find its value unchanged if not what should i do to make it happen.

推荐答案

你必须使用指向顶层结构的指针。如果不是顶层结构是按值复制的,但是所有指针值都变得很粗糙。



尽量避免这样的嵌套它不是好风格。得到一个finner和更好的模块化。
You must work with pointers to the top structure. If not the top struct is copyied by value, but all values which are pointers get bogous.

Try to avoid such nesting it isnt good style. Get to a finner and better modularity.


如果你的页面的意思是文件,那么也许我明白你的问题是什么。

你想访问来自不同模块的相同结构。如果是这样,您只需要在一个模块中定义您的结构,而不是在您想要访问相同结构的所有其他模块中使用对外部数据的引用。不要使用'static'限定符,否则你的结构将无法从其他模块访问。

请参阅此示例:

1.创建一个头文件,您将在其中定义结构的原型:

If the meaning of page for you is file then maybe I understood what your problem is.
You want to access to same structure from different modules. If so you need to define your structure in only one module, than use a reference to 'external' data in all other modules where you want access same structure. Don't use 'static' qualifier or your struct will not be accessible at all from other modules.
See this example:
1. Create an header file where you will define the protos of your structures:
// header file "header.h" where you define structures

typedef struct _nestedfirst    //define inner structure
{
    unsigned num;
} stNESTEDFIRST;

typedef struct _first
{
    struct _nestedfirst *nf;
    unsigned count;
} stFIRST;

extern stFIRST MyStFirstStructure;





2 。在您需要访问结构的所有文件中包含此标题,但仅在一个实例中包含结构:



2. Include this header in all files where you need access to the structure, but in only one instanciate the structure:

// file1.c
 #include <header.h>

stFIRST MyStFirstStructure;    //in file1.c we create the struct

int foo(void)
{
    MyStFirstStructure.nf->num = 0;
    ....
}







// file2.c
 #include <header.h>

int bar(void)
{
    MyStFirstStructure.nf->num += 2;
    ....
}





请注意虽然存储属性适用于整个结构,意味着结构中的所有变量都获得了这样的属性,在你的结构中,你已经向结构声明了指针,而不是结构!

所以指针仍然是永久的并且无限期地保存您设置的值,但指针引用的嵌套结构具有它自己的存储类,具体取决于您在其声明中的内容。



Please take into account that while the storage attribute applies to the whole structure, meaning that all variables inside the structure get such attribute, in your case in your structure you have declared a pointer to a struct, not a struct!
So the pointer still is permanent and holds indefinitely the value you have set into it, but the nested structure referred by the pointer have its own storage class depending on what you put in its declaration.


在询问此类问题之前,我是否可以建议您查阅文档,然后您应该自己使用调试器进行测试。如果您现在不知道如何使用调试器,那么就是学习的时间。



您对单词页面的使用令人困惑,因为您实际上是指文件或翻译单元 http://en.wikipedia.org/wiki/Translation_unit_%28programming%29 [ ^ ]。



现在我们来看看: https://msdn.microsoft.com/en-us/ library / s1sb61xd.aspx [ ^ ]

我们学习:

•在文件范围(全局和/或命名空间范围)声明变量或函数时,static关键字指定变量或函数具有内部链接。声明变量时,变量具有静态持续时间,编译器将其初始化为0,除非您指定另一个值。



内部链接仅在您声明的文件中可见,静态持续时间表示程序生命周期内的生命。



所以是的,如果你声明它是静态的,它会在程序的整个生命周期中保留它的价值。



你将需要类似的东西:



Can I suggest that before asking this kind of question you should consult documentation and then you should do a test yourself using your debugger. If you don't know how to use the debugger now is the time to learn.

Your use of the word page is confusing as you actually mean file or translation unit http://en.wikipedia.org/wiki/Translation_unit_%28programming%29[^].

Now if we look here:https://msdn.microsoft.com/en-us/library/s1sb61xd.aspx[^]
we learn:
•When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.

Internal linkage means only visible in the file you declare it and static duration means lives for the life of the program.

So yes if you declare it static it will retain its value throughout the life of the program.

You will need something like:

static struct first *f = new first();
first->count = 12;
first->nf = new nestedfirst();


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

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