如何声明函数指针的头和c-文件? [英] How to declare function pointer in header and c-file?

查看:90
本文介绍了如何声明函数指针的头和c-文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在如何在头文件中声明一个函数指针有点困惑。
我想用它在主,一个名为menus.c和menus.h我想声明。
我们要初始化指向一个特定的功能。

I'm a little confused over how to declare a function pointer in a header file. I want to use it in main and a file called menus.c and declare it in menus.h I assume. We want to initialize to point to a certain function.

它看起来是这样的:

void (*current_menu)(int);

什么是我们写menus.c,menus.h和主?

What do we write in menus.c, menus.h and main?

推荐答案

一个函数指针仍然是一个指针,这意味着它仍然是一个变数。

A function pointer is still a pointer, meaning it's still a variable.

如果你想有一个变量是可见的从多个源文件,最简单的解决方法是把它声明的extern 在头,其他地方的定义。

If you want a variable to be visible from several source files, the simplest solution is to declare it extern in a header, with the definition elsewhere.

在头:

extern void (*current_menu)(int);

在一个源文件:

void (*current_menu)(int) = &the_func_i_want;

这篇关于如何声明函数指针的头和c-文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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