Re: allowing a function to be called only from a specific function



Harald van D?k wrote:

CBFalconer schreef:
Harald van D?k wrote:
CBFalconer wrote:

... snip ...

Yes it can. The 'static' only prevents visibility outside the
compilation unit.

No, it cannot. f3 is defined before f1 is declared. You can refer
to a static function or variable with an extern declaration if and
only if the a static declaration is already in scope, and you
cannot use static on a function declaration with block scope.

Counter example. Nothing says you have to heed warnings. Proper
isolation requires a separate source file.

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(void) {
void f1(void);

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}

[1] c:\c\junk>cc junk.c
junk.c:10: warning: `f1' was declared `extern' and later `static'

[1] c:\c\junk>.\a
In f1

That's nice. As far as standard C is concerned, the behaviour is
undefined per 6.2.2p7, and my compiler rejects it. If you count
implementation-specific extensions, then inline assembly may also allow
you to access static functions defined in another source file.

Then try this:

#include <stdio.h>
static void f1(void);

int main(void) {

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

.



Relevant Pages