Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!columbia!cheshire.columbia.edu!francus From: francus@cheshire.columbia.edu (Yoseff Francus) Newsgroups: comp.lang.c Subject: Re: C pointer to a function Message-ID: <5017@columbia.edu> Date: Sat, 26-Sep-87 20:33:48 EDT Article-I.D.: columbia.5017 Posted: Sat Sep 26 20:33:48 1987 Date-Received: Sun, 27-Sep-87 11:38:29 EDT References: <501@acf3.NYU.EDU> Sender: nobody@columbia.edu Reply-To: francus@cheshire.columbia.edu.UUCP (Yoseff Francus) Distribution: na Organization: Columbia University CS Department Lines: 48 Keywords: static pointer function 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