Pointers and Static Variables

It is legal to return a pointer from a function to a static local variable.

A static local variable exists for the lifetime of the program.

Beware, a subsequent call to the function may change the variable.

struct a *f(..) {
    static struct a x;
    ....
    return &x; /* ok */
}

Similarly for top-level variables:

char a[10];
char *f(..) {
    ....
    return &a; /* ok */
}

Index