A series of tests on function pointers in C language and a series of function pointers

Source: Internet
Author: User
Tags mul

A series of tests on function pointers in C language and a series of function pointers
1. Start with a simple summation product function.


#include <stdio.h>int add(int a , int b){    return a + b;}int mul(int a , int b){    return a * b;}int main(){    int a_count = add(5,7);    int m_count = mul(5,7);    printf("a_count is %d\n",a_count);    printf("m_count is %d\n",m_count);        return 0;}




Output:


A_count is 12

M_count is 35

Program ended with exit code: 0




2. Try the function pointer.


# Include <stdio. h> int add (int a, int B) {return a + B;} int mul (int a, int B) {return a * B;} int main () {int (* p_add) (int, int); // declare the function pointer p_add = add; int (* p_mul) (int, int); p_mul = mul; int a_count = p_add (5, 7); int m_count = p_mul (5, 7); printf ("a_count is % d \ n", a_count ); printf ("m_count is % d \ n", m_count); return 0 ;}




Result unchanged


A_count is 12

M_count is 35

Program ended with exit code: 0




3. simplify the definition of function pointers


# Include <stdio. h> int add (int a, int B) {return a + B;} int mul (int a, int B) {return a * B;} int main () {typedef int (* fun) (int, int); // define the type fun is a pointer to the function fun p_add = add; fun p_mul = mul; int a_count = p_add (5, 7); int m_count = p_mul (5, 7); printf ("a_count is % d \ n", a_count ); printf ("m_count is % d \ n", m_count); return 0 ;}



Result unchanged


A_count is 12

M_count is 35

Program ended with exit code: 0




4. Try to use the function pointer Array


# Include <stdio. h> int add (int a, int B) {return a + B;} int mul (int a, int B) {return a * B;} int main () {int (* func []) (int, int) = {add, mul}; // define the function pointer array int a_count = func [0] (5, 7 ); int m_count = func [1] (5, 7); printf ("a_count is % d \ n", a_count); printf ("m_count is % d \ n", m_count ); return 0 ;}


Result unchanged

A_count is 12

M_count is 35

Program ended with exit code: 0




5. Improved function pointer Array


After all, when there are more functions, who can remember the number of multiplication in the array and the subscript is.


# Include <stdio. h> int add (int a, int B) {return a + B;} int mul (int a, int B) {return a * B;} int main () {int (* func []) (int, int) = {add, mul}; // defines the function pointer array enum func_tpye {ADD, MUL }; // The Order is consistent with the preceding int a_count = func [ADD] (5, 7); int m_count = func [MUL] (5, 7); printf ("a_count is % d \ n ", a_count); printf ("m_count is % d \ n", m_count); return 0 ;}


Result unchanged



A_count is 12

M_count is 35

Program ended with exit code: 0




6. Call different functions in the function


In the previous example, it is better to use functions directly.


# Include <stdio. h> typedef int (* fun) (int, int); int add (int a, int B) {return a + B;} int mul (int a, int B) {return a * B;} int add_mul (fun f, int a, int B) {return f (a, B);} int main () {int (* func []) (int, int) = {add, mul}; // defines the function pointer array enum func_tpye {ADD, MUL }; // The Order is the same as that in printf ("a_count is % d \ n", add_mul (func [ADD], 5, 7 )); printf ("m_count is % d \ n", add_mul (func [MUL], 5, 7); return 0 ;}


Result unchanged




A_count is 12

M_count is 35

Program ended with exit code: 0




7. The above example can be expanded to be more powerful.


For example, you can replace the multiplication of two numbers with the multiplication of multiple numbers.


# Include <stdio. h> # include <stdarg. h> typedef int (* fun) (int, int); int add (int a, int B) {return a + B;} int mul (int a, int B) {return a * B;} int add_mul (fun f, int n ,...) {va_list ap; va_start (ap, n); int count = va_arg (ap, int); for (int I = 0; I <(n-1); ++ I) {count = f (count, va_arg (ap, int);} return count;} int main () {int (* func []) (int, int) = {add, mul}; // define the function pointer array enum func_tpye {ADD, MUL}; // keep the order consistent with the preceding printf ("a_count is % d \ n ", add_mul (func [ADD],); printf ("a_count is % d \ n", add_mul (func [ADD )); printf ("m_count is % d \ n", add_mul (func [MUL], 5, 1, 2, 3, 4, 5); printf ("m_count is % d \ n ", add_mul (func [MUL], 3,7, 3,6); return 0 ;}


The first parameter is a function, the second parameter is the number of operations, followed by an indefinite parameter.

Result:



A_count is 15

A_count is 16

M_count is 120

M_count is 126

Program ended with exit code: 0







How does C make a function return a pointer?

This method is called "using the pointer function * creat () return value to transmit dynamic memory", which is a C syntax

First, you should note that the subfunction * creat () uses malloc to dynamically apply for memory, and return returns the address pointed to by the pointer variable, not the pointer! It is equivalent to returning the dynamic memory you applied to the main function.

Int * fun (void) // here is the pointer Function
{
Int * p = (int *) malloc (int); // dynamically applied memory // p points to a valid memory address here
Return p;
}

Void main (void)
{
Int * q = NULL;
Q = fun (); // fun () returns the address pointed to by the pointer p (the pointer p is created on the stack, so * fun () function is running, the memory allocated by the pointer is automatically released by the system. At this time, q points to p and the memory applied for by malloc is obtained.
Free (q); // releases the dynamic memory.
}

How does C declare a function that returns a function pointer?

Typedef int (* AA )();
AA function ();

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.