doing nasty things with internal static variables

Noam Mendelson c60b-1eq at e260-1c.berkeley.edu
Wed Mar 20 15:44:45 AEST 1991


In article <1991Mar19.183920.18911 at rice.edu> fontenot at comet.rice.edu (Dwayne Jacques Fontenot) writes:
>I have found myself doing this because it works, but I am curious if it
>is a common practice or if it is highly likely to get me into trouble:
>
>char *foo()
>{
>  static char string[64];
>
>  ...
>  return(string);
>}

This is fine.  Since the area of memory occupied by the variable string
is guaranteed not to be touched, you can assume that string will have
the same value until it is directly modified.  I, in fact, use this
construct all the time.  It is extremely convenient.

===============================================================
Noam Mendelson                       | "I haven't lost my mind,
c60b-1eq at web.Berkeley.EDU            |  it's backed up on tape
University of California at Berkeley |  somewhere."



More information about the Comp.lang.c mailing list