Are strings local?

Tommy Levitte pk-tle at nada.kth.se
Wed Aug 3 19:07:39 AEST 1988


>I've got a question about string constants.  According to the first
>edition of K&R, stuff of the form "this is a string" is static (page 181).
>This means they are "local to a block" (definition of static--page 182).
>Okay, so does this mean that you can't do this:
>
>	char *some_function()
>	{
>	  char *ptr;
>	  ptr= "string";
>	  return(ptr);
>	}
>
>It seems to me that "string" would only be defined inside some_function.
>Is that right, or am I missing something subtle?

There are two kinds of statics:

 - External statics.
 - Internal statics.

External statics are defined outside functions, and internal are defined inside
functions.
External statics are reachable in the whole file, but internal statics are only
available in the function in which they are defined.

A string constant is an exception to this rule. It is available in the whole
file. This means that all your examples where legal.

-- 
-------------------------------------------------------------------------------
Tommy Levitte (pk-tle at draken.nada.kth.se or gizmo at kicki.stacken.kth.se)
-------------------------------------------------------------------------------



More information about the Comp.lang.c mailing list