简体   繁体   中英

Retaining Member values of structure across function call

I was wondering if there is any way we can retain the member values of a structure across function calls in case the structure is a local. In case of primitive data types, we declare them as static and data will be retained across function calls. If structure defined in calling portion or as a global variable, I know we can retain this. But just curious if there are any other alternative to retain local structure values across function calls.

struct A
{
    int mem1;
}

void f1(void)
{
    struct A var1;
/*Now this var1 is a local variable. Declaring it as static will not help to retain value of mem1 across function calls I guess. */
}

As stated in wikipedia , and just to set the stage:

...static is a reserved word controlling both lifetime (as a static variable) and visibility (depending on linkage). The word static is also used in languages influenced by C, such as Java.

Static is defined as a storage class. A storage class defined the scope and life-time of variables and/or functions. A storage class precedes the type. Regardless of whether it is primitive or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM