Simple function pointer example

Source: Internet
Author: User
  1. # Include <stdio. h>
  2. Typedef void (* bottle_type) (int I); // we have designed a bottle model here. This bottle is not easy. You can install any starting function address! Let's use it to create a bottle.
  3. Void vinegar (int I) // vinegar
  4. {
  5. Printf ("% d: Now, I am vinegar, not bottle! /N ", I );
  6. }
  7. Void soy (int I) // soy sauce
  8. {
  9. Printf ("% d: Now, I am soy, not bottle and vinegar! /N ", I );
  10. }
  11. Int main ()
  12. {
  13. Bottle_type bottle; // a bottle is created based on the bottle_type model.

  14. Bottle = vinegar; // Add vinegar to the bottle. Will you still call it a bottle? If yes, I don't believe it. If you go to the supermarket to buy vinegar, you will point to the vinegar and say to the vending lady, I want to buy this bottle ^-^! (Actually, the vinegar address is installed in the bottle)
  15. (* Bottle) (1); // because the bottle contains vinegar, the result is also vinegar.
  16. Bottle = soy; // It's too sour to add vinegar now! Add soy sauce. Will you still call it a bottle or vinegar? It's called soy sauce (soy address is installed in the bottle)
  17. (* Bottle) (2); // bottle is soy sauce.
  18. Return 0;
  19. }

Liulife @ liulife :~ /Test $ gcc-O test_func_p test_func_p.c

Liulife @ liulife :~ /Test $ ls
Test_func_p test_func_p.c

Liulife @ liulife :~ /Test $./test_func_p
1: Now, I am vinegar, not bottle!

2: Now, I am soy, not bottle and vinegar!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.