Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!oddjob!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: C pointer to a function Message-ID: <8804@mimsy.UUCP> Date: Mon, 28-Sep-87 09:01:32 EDT Article-I.D.: mimsy.8804 Posted: Mon Sep 28 09:01:32 1987 Date-Received: Tue, 29-Sep-87 04:26:09 EDT References: <501@acf3.NYU.EDU> Distribution: na Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 59 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. 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