Function Pointers -----PART 1

Introduction
  
In a previous series of articles named " Pointer Perfect," I looked at how data stored in computer memory is accessible through pointer, The code you write for your application in the form of functions (these can be global or class member functions) is very much accessible through pointers in the same way, and i will introduce you to the syntax and usage of these function pointers in this article.
Function pointers are great for implementing callbacks and are very useful when you want to introduce late binding in your application.They help you parse configuration files,help you read protocols and can help you store a function call in an object when you want to delay or record the call(very useful in an undo/redo system).
A Quick Example
There is nothing like a quick example to take you straight to the heart of a topic. Here is a simple code example that use a function pointer.
// main.cpp
    #include
    int IncOne(int var){
         return ++var;
    }
    int main(int argc,char *argv[])
    {
          //creating pointer to function
         int (*inc_one)(int)=&IncOne;
         //use the function the standard way
         int result=IncOne(1);
        (void) printf("result is :%d/n",result);
        //utilize that function pointer
         result =inc_one(1);
        (void) printf("result is : %d/n",result);
        return 0;
   }
 When you run this simple example, you will notice that both times the resulted is '2'-- as expected,of course, So what has the compiler come up with for us? Let's take a peek into the debugger at the value stored in inc_one.
       int (*inc_one) (int)=&IncOne;
      00411A9E   mov  dword ptr [line_one],offset IncOne     (411523h)
 It holds the address 0x00411523,add loking at the memory at that position we find the following line:
    








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值