Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
#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;
}
Posted

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:
C++
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.
 
Share this answer
 
v4
Comments
RajneeshSaysHello 14-May-15 0:55am    
ok so i have tried your code but am unable to set values of mystruct in the function when they are declared static, how would i do that beside setting there values at compile time.
thanks for ans.
Frankie-C 14-May-15 3:11am    
Dear Rajneesh I thinnk that you have not fully understood what I said. This maybe is in part my fault so I'll try to be more clear.
In my example:
------------------------------------------------------------------------------
typedef _tagMyStruct
{
int a;
struct _tag_Struct2
{
float c;
int d
} MySecondStruct;
} MyExampleStruct;
---------------------------------------------------------------------------------
The inner struct _tag_Struct2 is fully defined *inside* the main structure _tagMyStruct and int this case also struct _tag_Struct2, with all his members, will be statically allocated.

The way you declared your structure:
--------------------------------------------------------------------------------
struct eg {
struct nestedstruct *nsvar;
int n;
};
--------------------------------------------------------------------------------
The structure itself will be allocated statically (permanent), same allocation will have the integer n and the *pointer* to structure nestedstruct. The pointer, *not* the structure nestedstruct. If you want to make nestedstruct static also you have to insert in your structure the whole nested structure, not a pointer to it!
Is it clear now?
RajneeshSaysHello 14-May-15 3:54am    
yes, thanks i will try with that.
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.
 
Share this answer
 
Comments
RajneeshSaysHello 13-May-15 4:48am    
i was also wondering about any downside to that approach related to memory or any better way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900