[comp.lang.c] C pointer to a function

hazzah@acf3.NYU.EDU (Ali Hazzah) (09/26/87)

	Consider the function foo, defined as:

	static char *foo(name)
	char *name;
	{
		static char path[20];
		char *strcat();

		...

		return(strcat(path, name);
	}

	and invoked in the following manner:

	callfoo()
	char *name;
	{
	char *path, *foo();

	...

	path = foo(name);

	...

	}

	The question here concerns the scope of the pointer to
	the function foo.
	Specifically, why might the pointer be defined as static?
	(An example of this can be found on p.23 of Marc Rochkind's
	book, "Advanced Unix Programming", 4th printing.)
	

francus@cheshire.columbia.edu (Yoseff Francus) (09/27/87)

In article <501@acf3.NYU.EDU> hazzah@acf3.NYU.EDU (Ali Hazzah) writes:
>	Consider the function foo, defined as:
>
>	static char *foo(name)
>	char *name;
>	{
>		static char path[20];
>		char *strcat();
>		...
>		return(strcat(path, name);
>	}
>
>	and invoked in the following manner:
>
>	callfoo()
>	char *name;
>	{
>	char *path, *foo();
>	path = foo(name);
>	}
>	The question here concerns the scope of the pointer to
>	the function foo.
>	Specifically, why might the pointer be defined as static?
>	(An example of this can be found on p.23 of Marc Rochkind's
>	book, "Advanced Unix Programming", 4th printing.)

The reason its declared static is this: What you are passing back from
the function is a memory location. The variable that you hope this
location is a pointer to is the variable path in the function. This
variable is declared locally in the function. Therefore, when you return
to callfoo(), there is no guarantee that there will be any reasonable 
data in the memory location that has been returned to you.  However, since
its been declared as static the data has to remain at that memory location
since static guarantees that if you go back into foo(), the data pointed to
by path will still be there.  Since you are accessing the memory location
of path (from foo()), and you have declared it static, callfoo() will be
able to get at the value properly.




******************************************************************
yf
In Xanadu did Kubla Khan a stately pleasure dome decree
But only if the NFL to a franchise would agree.

ARPA: francus@cs.columbia.edu
UUCP: seismo!columbia!francus

guy@gorodish.UUCP (09/27/87)

> 	The question here concerns the scope of the pointer to
> 	the function foo.
> 	Specifically, why might the pointer be defined as static?

Which "pointer to the function foo"?  There are no pointers to functions
anywhere in your example (nor are there any in Rochkind's example, either).
The only things declared as static here are the function "foo" (

	static char *foo(name)

does NOT declare a function pointer, it declares a function that *returns* a
pointer) and the variable "path".

The reason why "path" is declared "static" was given by another poster; it
cannot be automatic, because a pointer to it is returned by "foo", but when
"foo" returns "path" will be discarded and the pointer to it will no longer be
a pointer to a valid object.

The reason why "foo" is declared "static" is that it is presumably not used by
any code outside the module in which it is defined, and it is good programming
practice not to export anything from a module unless it is to be used by code
outside that module - in other words, declare everything with file scope
"static" unless you can't do so.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

chris@mimsy.UUCP (Chris Torek) (09/28/87)

In article <501@acf3.NYU.EDU> hazzah@acf3.NYU.EDU (Ali Hazzah) writes:
>Consider the function foo, defined as:

>static char *foo(name)
>char *name;
>{
>	static char path[20];
>	char *strcat();
>	...
>	return(strcat(path, name));
>}

>and invoked in the following manner:

>callfoo()
>char *name;
>{
>char *path, *foo();
>...
>path = foo(name);
>...
>}

>The question here concerns the scope of the pointer to
>the function foo.

The question is ill-phrased: there is no pointer to function foo.
The only pointers declared above are pointers to char.

>Specifically, why might the pointer be defined as static?

There are two `static's in the code above.  The first is in
the declaration of function `foo',

	static char *foo(name) ...

and the second in the declaration of `path':

		static char path[20];

The two `static's have entirely different effects.  The former is
applied to a function, and all functions are static anyway:  no
function is compiled onto the run-time stack.  Outside the body of
a function, the word `static' changes meaning to `limited scope of
visibility': the function `foo' can be called only from the source
file that contains it.  The second `static' has the more expected
meaning of placing `path' in a fixed place, not on the stack; this
is done so that the value returned from function foo is not
immediately overwritten by any other stack operations.  Without
this `static', a call such as

	p = foo(name);
	printf("p = %s\n", p);

is quite likely to print rubbish, the string to which p points
having been overwritten by the call to printf().
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

ftw@datacube.UUCP (09/28/87)

> francus@cheshire.columbia.edu.UUCP writes:
> In article <501@acf3.NYU.EDU> hazzah@acf3.NYU.EDU (Ali Hazzah) writes:
> >	Consider the function foo, defined as:
> >
> >	static char *foo(name)
> >	char *name;
> >	{
> >		static char path[20];
> >		char *strcat();
> >		...
> >		return(strcat(path, name);
> >	}
> >
> >	and invoked in the following manner:
> >
> >	callfoo()
> >	char *name;
> >	{
> >	char *path, *foo();
> >	path = foo(name);
> >	}
> >	The question here concerns the scope of the pointer to
> >	the function foo.

"foo" is not a pointer to a function, it is a function returning a pointer.


> >	Specifically, why might the pointer be defined as static?

This is a fine point:  What foo is is a static function that returns a pointer
to a char, not a "function returning a static pointer".  What "static" means
when applied to a function definition like that is that the scope of the
function is only within that source code file.  If I have "foo.c" and "bar.c",
and foo.c has a "static foo()" in it, I cannot call the function foo() from
bar.c

> >	(An example of this can be found on p.23 of Marc Rochkind's
> >	book, "Advanced Unix Programming", 4th printing.)
> 
> The reason its declared static is this: What you are passing back from
> the function is a memory location. The variable that you hope this
> location is a pointer to is the variable path in the function. This
> variable is declared locally in the function. Therefore, when you return
> to callfoo(), there is no guarantee that there will be any reasonable 
> data in the memory location that has been returned to you.  However, since
> its been declared as static the data has to remain at that memory location
> since static guarantees that if you go back into foo(), the data pointed to
> by path will still be there.  Since you are accessing the memory location
> of path (from foo()), and you have declared it static, callfoo() will be
> able to get at the value properly.
> 

The above explains why the array "path" in the function foo is declared as
static.

> 
> ARPA: francus@cs.columbia.edu
> UUCP: seismo!columbia!francus
				Farrell T. Woods 

Datacube Inc. Systems / Software Group	4 Dearborn Rd. Peabody, Ma 01960
VOICE:	617-535-6644;	FAX: (617) 535-5643;  TWX: (710) 347-0125
UUCP:	ftw@datacube.COM,  ihnp4!datacube!ftw
	{seismo,cbosgd,cuae2,mit-eddie}!mirror!datacube!ftw

eao@anumb.UUCP (e.a.olson) (09/29/87)

> 
> 	Consider the function foo, defined as:
> 
> 	static char *foo(name)
> 	char *name;
> 	{
> 		static char path[20];
> 		char *strcat();
> 
> 		...
> 
> 		return(strcat(path, name);
> 	}
> 
> 	and invoked in the following manner:
> 
> 	callfoo()
> 	char *name;
> 	{
> 	char *path, *foo();
> 
> 	...
> 
> 	path = foo(name);
> 
> 	...
> 
> 	}
> 
> 	The question here concerns the scope of the pointer to
> 	the function foo.
> 	Specifically, why might the pointer be defined as static?
> 	(An example of this can be found on p.23 of Marc Rochkind's
> 	book, "Advanced Unix Programming", 4th printing.)
> 	

    Well, one good reason  might be that the function is
    considered part of the data structure and that static
    is used to hide it. :-)
	7