其他文件中的静态函数访问 [英] Static function access in other files

查看:10
本文介绍了其他文件中的静态函数访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 static 定义的函数是否有可能在文件范围之外被访问?

Is there any chance that a function defined with static can be accessed 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
", pf());
    return 0;
}
$ gcc -ansi -pedantic -W -Wall f1.c f2.c
$ ./a.out
42

这篇关于其他文件中的静态函数访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆