C language: the function pointer is used to add, subtract, multiply, and divide two numbers (the function pointer is used as a parameter ).

Source: Internet
Author: User

C language: the function pointer is used to add, subtract, multiply, and divide two numbers (the function pointer is used as a parameter ).

//

// Main. c

// Function_pointer

//

// Created by mac on 15/8/2.

// Copyright (c) 2015 bjsxt. All rights reserved.

// Requirement: Use the function pointer as a parameter to calculate the sum, difference, product, and Quotient of two integers.

 

// Knowledge point: a function pointer is a pointer to a function. It points to the function to be called to complete the operation. In fact, this pointer is the entry address pointing to the function.

// Remember: the function to be called must be the same as the declared function pointer (including the return value type, number of parameters, and type)

# Include <stdio. h>

Int add (int, int );

Int sub (int, int );

Int mult (int, int );

Int divi (int, int );

Int main (int argc, const char * argv [])

{

Int function (int (* p) (int, int); // set the function pointer as a parameter.

// P is a pointer variable pointing to a function. It can point to a function with an integer type and two integer parameters. The p type is represented by int (*) (int, int ).

Printf ("add = % d \ n", add (20, 10 ));

Printf ("sub = % d \ n", sub (20, 10 ));

Printf ("mult = % d \ n", mult (20, 10 ));

Printf ("divi = % d \ n", divi (20, 10 ));

Return 0;

}

Int add (int a, int B)

{

Return a + B;

}

Int sub (int a, int B)

{

Return a-B;

}

Int mult (int a, int B)

{

Return a * B;

}

Int divi (int a, int B)

{

Return a/B;

}

Related Article

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.