功能指针的外部数组(Extern array of pointers to function)

编程入门 行业动态 更新时间:2024-10-11 09:29:12
功能指针的外部数组(Extern array of pointers to function)

首先是一点背景。 我的程序有一个标题, work.h 。 这个头包含一个结构,一些函数定义,以及指向我的基函数的extern指针数组。

work.h

typedef struct _foo { int id; char something[20]; } foo; typedef void (*pointer_function)(foo *); void do_first_to_foo(foo *); void do_second_to_foo(foo *); void do_third_to_foo(foo *); extern pointer_function base_functions[3];

然后一个名为work.c的程序与函数体,然后是主程序work.c 在头文件work.h中观察我已经定义了三个函数的原型,并且数组的大小是3,因此extern数组上的指针将指向3个函数中的每一个。

我的问题是,如何将extern数组的指针与三个函数相关联,其次,我需要在哪个文件中( work.c或main.c )。

我理解这个关联,我需要在文件work.c ,但没有别的。

A little bit of context first. My program features a header, work.h. This header contains a structure, some function definitions, and an extern array of pointers to my base functions.

work.h

typedef struct _foo { int id; char something[20]; } foo; typedef void (*pointer_function)(foo *); void do_first_to_foo(foo *); void do_second_to_foo(foo *); void do_third_to_foo(foo *); extern pointer_function base_functions[3];

Then a program called work.c with the bodies of the functions, and then the main program main.c. Observe in the header work.h that I have defined prototypes of three functions, and the size of the array is 3, so the pointers on the extern array will point to each one of the 3 functions.

My questions are, how I can associate the pointers of the extern array with the three functions, and second, in which file I need to do it (work.c or main.c).

I understand this association I need to do it in the file work.c, but nothing else.

最满意答案

在work.c :

#include "work.h" pointer_function base_functions[] = { do_first_to_foo, do_second_to_foo, do_third_to_foo };

说明:该数组名称是全局符号,需要在一个编译单元( .c文件生成的.o文件)中实际定义。 如果您没有它,链接器将找不到它。 如果您有多个副本,链接器会抱怨。 您只需要一个,与全局导出函数(未标记为内联)相同。

编辑:添加了显示#include "work.h" ,这很重要,因为它允许编译器检查extern声明是否与实际定义匹配。 如果你把它留下来,一切都会编译和链接没有抱怨,但你没有迹象表明是否存在不匹配,当其他编译单元中使用变量时,这可能会破坏内存中的损坏的其他数据。

In work.c:

#include "work.h" pointer_function base_functions[] = { do_first_to_foo, do_second_to_foo, do_third_to_foo };

Explanation: that array name is a global symbol, and needs to be actually defined in just one compilation unit (.o file produced from .c file). If you do not have it, linker will not find it. If you have multiple copies, linker will complain. You need just one, same as with global exported functions (which are not marked inline).

Edit: Added showing #include "work.h", which is important because it allows compiler to check that extern declaration matches the actual definition. If you leave it out, everything will compile and link without complaints, but you get no indication if there's a mismatch, which could wreak havoc like corrupt other data in memory when variable is used in other compilation units.

更多推荐

本文发布于:2023-07-06 07:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047450.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   指针   功能   Extern   pointers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!