免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 4713 | 回复: 21
打印 上一主题 下一主题

为什么编不过 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-26 15:56 |只看该作者 |倒序浏览
本帖最后由 macroideal 于 2011-03-26 16:06 编辑

int store_addr(long add)
{   
         
}   
void test_function()
{   

        printf("this is a big test\n");     
             
}  

store_addr( *(long*)&test_function ) ;  

VC2005
愣是不明白, 你试试

论坛徽章:
0
2 [报告]
发表于 2011-03-26 16:16 |只看该作者
int store_addr(long add)
{   
         
}   
void test_function()
{   

        printf("this is a big t ...
macroideal 发表于 2011-03-26 15:56



    你是想傳入 類型是 函數指針麽?

论坛徽章:
0
3 [报告]
发表于 2011-03-26 16:22 |只看该作者
你是想傳入 類型是 函數指針麽?
赤腳大仙 发表于 2011-03-26 16:16



    想把函数的指针存下来, 貌似C不支持在main外边掉用函数或对变量赋值...杯具

论坛徽章:
0
4 [报告]
发表于 2011-03-26 16:22 |只看该作者
int store_addr(long add)
{   
         
}   
void test_function()
{   

        printf("this is a big t ...
macroideal 发表于 2011-03-26 15:56

試驗下面的看看。
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef void(*proc_type)();

  4. int store_addr(long add)
  5. {   
  6.     ((proc_type)add)();
  7. }   

  8. int store_addr_ex(proc_type func)
  9. {
  10.         if (func)
  11.                 func();
  12. }

  13. void test_function()
  14. {   

  15.         printf("this is a big test\n");     
  16.             
  17. }

  18. int main(int argc, char* argv[])
  19. {
  20.         store_addr(test_function ) ;  
  21.         store_addr_ex(test_function) ;         

  22. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2011-03-26 16:23 |只看该作者
想把函数的指针存下来, 貌似C不支持在main外边掉用函数或对变量赋值...杯具
macroideal 发表于 2011-03-26 16:22



    可以的。直接調用或賦值都行。

论坛徽章:
0
6 [报告]
发表于 2011-03-26 16:24 |只看该作者
想把函数的指针存下来, 貌似C不支持在main外边掉用函数或对变量赋值...杯具
macroideal 发表于 2011-03-26 16:22



    函数调用或赋值之类的,只能出现在函数内(不管是不是MAIN)。在任何函数之外,只能定义或声明

论坛徽章:
0
7 [报告]
发表于 2011-03-26 16:27 |只看该作者
想把函数的指针存下来, 貌似C不支持在main外边掉用函数或对变量赋值...杯具
macroideal 发表于 2011-03-26 16:22
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef void(*proc_type)();

  4. int store_addr(long add)
  5. {   
  6.     ((proc_type)add)();
  7. }   

  8. int store_addr_ex(proc_type func)
  9. {
  10.         if (func)
  11.                 func();
  12.                
  13.         return 1;
  14. }

  15. void test_function()
  16. {   

  17.         printf("this is a big test\n");     
  18.             
  19. }

  20. void test_function_ex()
  21. {   

  22.         printf("this is a big test ex\n");     
  23.             
  24. }

  25. static proc_type my_proc_list[] = {test_function, test_function_ex, 0};

  26. int main(int argc, char* argv[])
  27. {
  28.         int i;

  29.     for (i = 0; ; ++i) {
  30.                 if (!my_proc_list[i]) break;
  31.                
  32.                 my_proc_list[i]();
  33.         }
  34.         store_addr(test_function ) ;  
  35.         store_addr_ex(test_function) ;         
  36.        



  37. }
复制代码

论坛徽章:
0
8 [报告]
发表于 2011-03-26 16:27 |只看该作者
試驗下面的看看。
赤腳大仙 发表于 2011-03-26 16:22


   不是这个意思, 我想让写函数里, 不关心把这个函数指针

我的原意思如下:

  1. #define BEGIN_FUNCTION(f)   void  f(){
  2. #define END_FUNCTION(f)  };store_addr((long)f);  
  3. BEGIN_FUNCTION(test_function)
  4. {
  5.         printf("this is a big test\n");

  6. }
  7. END_FUNCTION(test_function)
  8. BEGIN_FUNCTION(test_function1)
  9. {
  10.         printf("this is a big test\n");

  11. }
  12. END_FUNCTION(test_function1)
复制代码

像这样, 谁函数不用关心存, 把我可以在别处调用这些函数.

论坛徽章:
0
9 [报告]
发表于 2011-03-26 16:29 |只看该作者
不是这个意思, 我想让写函数里, 不关心把这个函数指针

我的原意思如下:

像这样, 谁函数不用关 ...
macroideal 发表于 2011-03-26 16:27



    直接存到變量裏頭就行。

论坛徽章:
0
10 [报告]
发表于 2011-03-26 16:31 |只看该作者
直接存到變量裏頭就行。
赤腳大仙 发表于 2011-03-26 16:29



    其实我这个是个提供给别的文件, 别人只管定义自己的函数, 我根据条件调用他的函数...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP