Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cca!mirror!datacube!ftw From: ftw@datacube.UUCP Newsgroups: comp.lang.c Subject: Re: C pointer to a function Message-ID: <102600017@datacube> Date: Mon, 28-Sep-87 09:59:00 EDT Article-I.D.: datacube.102600017 Posted: Mon Sep 28 09:59:00 1987 Date-Received: Wed, 30-Sep-87 05:49:18 EDT References: <501@acf3.NYU.EDU> Lines: 65 Nf-ID: #R:acf3.NYU.EDU:-50100:datacube:102600017:000:2371 Nf-From: datacube.UUCP!ftw Sep 28 09:59:00 1987 > 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