静态函数访问其他文件 [英] Static function access in other files
本文介绍了静态函数访问其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有可能在文件范围之外评估静态函数。 ?
Is there any chance that Static function can be assessed outside the file scope. ?
推荐答案
这取决于访问的含义。当然,函数不能在任何其他文件中通过名称来调用,因为它在另一个文件中是 static
,但是你有一个函数指针。
It depends upon what you mean by "access". Of course, the function cannot be called by name in any other file since it's static
in a different file, but you have have a function pointer to it.
$ cat f1.c
/* static */
static int number(void)
{
return 42;
}
/* "global" pointer */
int (*pf)(void);
void initialize(void)
{
pf = number;
}
$ cat f2.c
#include <stdio.h>
extern int (*pf)(void);
extern void initialize(void);
int main(void)
{
initialize();
printf("%d\n", pf());
return 0;
}
$ gcc -ansi -pedantic -W -Wall f1.c f2.c
$ ./a.out
42
这篇关于静态函数访问其他文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文