函数指针的传递问题
来源:互联网 发布:windows 最高级别权限 编辑:程序博客网 时间:2024/05/01 11:02
#include <stdio.h>
typedef int (*func)(int);
int add (int a)
{
return ++a;
}
int getfunc(func myfunc)
{
myfunc = &add;
return 0;
}
int main()
{
int i;
func myfunc;
i = 10;
getfunc(myfunc);
printf(" a is %d/n", (*myfunc)(i));
return 0;
}
I can't get what i want.the result is " a is 0".why is that??
==================================================
I think you're actually lucky that you get a is 0
instead of a crash. The problem is that getfunc
takes the function pointer by value, so the myfunc = &add
inside getfunc
has no effect on the caller at all. Try
int getfunc(func *myfunc)
{
*myfunc = &add;
return 0;
}
and in main:
getfunc(&myfunc);
- 函数指针的传递问题
- C函数的指针传递问题
- 函数指针参数的传递问题
- 函数指针参数的传递问题
- 指针作为函数参数传递的问题
- C 函数值传递和指针传递的效率问题
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)(转)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)(转)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)(转)
- 函数参数的传递问题(指针的指针)(转)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)
- Java语言基础:基本数组类型
- 通过Axis开发Web Service
- 为了食油,声讨百度(2011-03-25 14:25:09)转载标签: 杂谈 --韩寒啊!
- .NET 常用目录获取
- /wMsg参数
- 函数指针的传递问题
- DIV repeater 全表格
- 应用程序在Linux上的执行过程
- 休眠过程中关掉LDO4.
- 如何判断一个机器是Little-Endian还是Big-Endian
- Linux系统下内存申请应用的方法原理总结
- shell中一些特殊符号的含义简介
- Windows7更新失败的一个解决办法
- Linux系统进程的几个用户ID及其转换方法