Are strings local?

Chad Gibbons chad at lakesys.UUCP
Tue Aug 2 21:26:47 AEST 1988


In article <644 at m10ux.UUCP> rgr at m10ux.UUCP (Duke Robillard) writes:
>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?

	When a string is declared inside a function or block, it is indeed
local to that function, and that function only. However, just like any other
variable, strings can be passed to functions, returned from functions,
compared, or what have you.
 
	[..so what about something like:]
>	char *some_function()
>	{
>	  another_function("string");
>	}
>
>[so this would be illegal,] right?  And we know that's okay, cause printf 
>does it.  What's the story?

	As I stated above, you an pass a string as your return value. There is
really no cause for alarm if I interperted your message correctly. Strings are
just another pointer, like any other variable. Quoting from "The C Programming
Language, Second Edition", Kernighan & Ritchie: "...because [static variables
declared in a function are local to that function,] no other function may have
direct access to them...each local variable in a function comes in existence
only when the function is called, and disappears when the function is exited."
Not that keypharse here is "direct access." As if true of all automatic
variables, no other program part can have "direct access" to that variable.
You an still freely pass that variable to and fro as you wish, however. 

	Consider the artifical sequence:

	char *testing(char *s, char *t);

	{...}

	char *testing(char *s, char *t)
	{
		if ((strcmp(s, t)) == 0) 
			return ("The strings are equal");
		else 
			switch (strcmp(s, t)) {
			case -1:
				return ("String s is < string t.");
				break;
			case  1: 
				return ("String s is > string t.");
				break;
			default:
				break;
			}
	}

	Here we have a function returning a string, with two string arguments.
String s and string t are passed from the calling function into testing which
uses the given pointer of the two strings. Both of these strings are then
passed to strcmp which lexigraphially compares the two strings and returns the
appropriate value. Depending on the returned value of strcmp, a string
constant is returned to the calling function.

	So you can see, strings, while being static - in most cases - within a
particular block, may be passed, compared, etc. in other functions, provided
the value is sent to that function/block.

	Hope this helped.

[Kernighan & Ritchie, The C Programming Language, Second Edition, Prentice
Hall publishing. Page 31, Section 1.10, Paragraph one.]


-- 
Chad Gibbons - UUCP:   {rutgers!uunet!uwvax}!lakesys!chad
Lake Systems - DOMAIN: chad at lakesys.UUCP
					-- "Red alert, Red alert"



More information about the Comp.lang.c mailing list