C language: the function pointer is used to add, subtract, multiply, and divide two numbers.

Source: Internet
Author: User

C language: the function pointer is used to add, subtract, multiply, and divide two numbers.

//

// Main. c

// Function_pointer

//

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

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

// Requirement: Calculate the sum, difference, product, and Quotient of two integers by using the function pointer.

 

// Knowledge point: a function pointer is a pointer to a function. It points to the function to be called to complete the operation.

// 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 (* p) (int, int); // defines the function pointer

P = add; // pointer to addition Function

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

P = sub; // pointer to the subtraction Function

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

P = mult; // pointer to multiplication function

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

P = divi; // pointer to Division Function

Printf ("divi = % d \ n", p (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.