简体   繁体   English

返回函数指针数组

[英]Return array of functions pointers

I have two files main.c and foo.c, I am trying to copy an array of functions from one file to another. 我有两个文件main.c和foo.c,我试图将一个函数数组从一个文件复制到另一个文件。

My problem is: Only the first item is getting copied. 我的问题是:只复制第一项。

This is my code: 这是我的代码:

foo.c: foo.c的:

void foo1();
void foo2();
void foo3();
void foo4();
void foo5();
void foo6();
void foo7();

void (*myFuncs[7])() = {
    foo1,
    foo2,
    foo3,
    foo4,
    foo5,
    foo6,
    foo7
};

void* getMyFuncs(){
    return myFuncs;
}

main.c: main.c中:

void (*things[7])();

void main(){
   memcpy(things, getMyFuncs(), sizeof(getMyFuncs()));
}

After running, in debug mode, I inspected my array things and only foo1 pointer has copied. 运行后,在调试模式下,我检查我的数组的东西 ,只有foo1指针已复制。

So, my output has: 所以,我的输出有:

void (*things[7])() = {
    foo1 (hex address here),
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00
};

What I expected: 我的期望:

void (*things[7])() = {
    foo1 (hex address here),
    foo2 (hex address here),
    foo3 (hex address here),
    foo4 (hex address here),
    foo5 (hex address here),
    foo6 (hex address here),
    foo7 (hex address here)
};

Why only the first item is getting copied? 为什么只复制第一项?

Thanks 谢谢

I found the solution, it was a silly mistake! 我找到了解决方案,这是一个愚蠢的错误!

My line: 我的专栏:

memcpy(things, getMyFuncs(), sizeof(getMyFuncs()));

is wrong, I don't want to copy the size of getMyFuncs(). 是错的,我不想复制getMyFuncs()的大小。

The write way to copy is by getting the size of my array of functions: 写入复制的方法是获取我的函数数组的大小:

memcpy(things, getMyFuncs(), sizeof(things));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM