当前位置:网站首页>[C language] pointer function, function pointer and array function

[C language] pointer function, function pointer and array function

2022-06-10 13:50:00 A egg of maker Association


Pointer function and function pointer are often confused in development .

Pointer function

 Format :  type  * Function name ()
return  Same value as function type ;

The body of a pointer function is a function , The type returned is pointer .
Is to point the pointer to the value returned by this function .

int*  f(int x,int y);	
// Because parentheses have a high priority 
// So it is first and foremost a possession x,y A function of two variables , Then one more int The pointer of type points to it .

The main body :f(int x,int y)

Functions can return numeric values , For normal functions, it should be like this :

int f(int x,int y)
{
    
	x=1;
	return x;
}
// here f() The return is x, The data type is int type 

Using a pointer function is like this ( Incorrect usage ):

int* f(int x,int y)
{
    
	x=1;
	return x;
}
// here f() The return is x The value of the address , amount to (int *)x;

This must be the wrong data , Because the pointer points to the past 1(x Value ) In the address of

The correct usage should be to point to with a pointer x The address you are in can be correct x To operate :

int* f(int x,int y)
{
    
	x=1;
	return &x;
}
// here f() The return is x Value , amount to (int *)&x

(int *)&x == x;

Small turtle pointer function analysis

The following is the code of the little turtle pointer function :
 Insert picture description here
One of them is to use the characteristics of pointer function : amount to char *(return “xxx”), A string can be used as a pointer , If not * Equivalent to char (return “xxx”), Only one character type is returned , added * Is equivalent to taking "xxx" The first address , The return is from xxx The value in the address .

Do not return a local variable of a function :

Note the scope of the local variable , Local variables can return addresses , But the data may no longer exist

A function pointer

 Format :  type  (*  Pointer name ) ( Parameters of the function to point to )

The body of a function pointer is a pointer , Is used to point to a function .
This pointer is used to point to a large area where this function is located .( The function name is the first address of the function )

int (*f) (int x,int y);	
// Because parentheses have a high priority .
//  So it is first of all a int (*f) The pointer to , Then point to the parameter (x,y) Function of .

The main body :int (*f)

Use function pointers as arguments

 Insert picture description here

int calc(int (*fp)(int,int),int num1,int num2);
calc(sub,3,5) The first parameter of : That is, use the function pointer to point to sub Address of function .
Start with the call , (*fp) This pointer is already sub Function .
namely (*fp)(int,int) == sub(int,int)

Data is from return (*fp)(num1,num2) Coming out , amount to sub(num1,num2)

Function pointer advanced

#include<stdio.h>

int add(int, int);
int sub(int, int);
int cal(int (*)(int, int), int, int);
int (*select(char a))(int, int);

int add(int num1, int num2)
{
    
	return num1 + num2;
}

int sub(int num1, int num2)
{
    
	return num1 - num2;
}

int cal(int (*fp)(int, int), int num1, int num2)
{
    
	return (*fp)(num1,num2);
}

int (*select(char op))(int num1, int num2)
{
    
	switch(op)
	{
    
		case '+':return add;
		case '-':return sub;
	}

}

int main()
{
    	
	int num1, num2;
	char op;
	//int (*fp)(int, int);
	printf("please enter a formular\n");
	scanf("%d %c %d", &num1, &op, &num2);
	printf("%d\n", cal(select(op), num1, num2));
	return 0;
}

Pay attention to cal A function is a function with three arguments , The first parameter is a function pointer .
select A function is a pointer function , Its return value is a function pointer .
When the input “1+2” The flow of this program is as follows . First “+” This symbol is read into op In this parameter , then op This parameter is passed in select function ,select Function switch Statement discovery op Is a plus sign , So it returns a point to add Function pointer to function , This function pointer is used as cal Function parameters are dereferenced after being passed in , And then num1 and num2 Pass in add Function to get the return value 3.

int (*select(char op))(int num1, int num2)
// It can be understood as : type  (*  A function pointer )( Parameters 1,  Parameters 2)
 call printf("%d\n", cal(select(op), num1, num2)); When ,
cal Can be called with function pointer select,select According to the passed parameters op,
 To choose which function to return add Or function sub The first address .

 The first address is finally transferred to int cal(int (*fp)(int, int), int num1, int num2) in ,
 By cat Function pointer of (int (*fp) To dereference .
 amount to (int (*fp)(return add)= function  add(num1,num2);

“ Array function ”( Digression )

stay c The concept of fishing array function in the language , But we can write array functions artificially ( No define)

 Format : int (*  Array )( The parameters of the function );  
int (* arr[8])(int a);

Array function cases :

original text

#include <stdio.h>
#include <stdlib.h>

#define EPSINON 0.000001 //  Define the allowable error 

double add(double x, double y);
double sub(double x, double y);
double mul(double x, double y);
double divi(double x, double y);

double add(double x, double y)
{
    
        return x + y;
}

double sub(double x, double y)
{
    
        return x - y;
}

double mul(double x, double y)
{
    
        return x * y;
}

double divi(double x, double y)
{
    
        //  Do not... On floating point numbers == or != Compare , because IEEE Floating point numbers are an approximation 
        if (y >= -EPSINON && y <= EPSINON)
        {
    
                printf(" The divisor cannot be zero 0\n");
                //  If the divisor is 0, call exit() Function to exit the program directly 
                exit(1);
        }
        else
        {
    
                return x / y;
        }
}

int main(void)
{
    
        int i;
        double x, y, result;
        double (*func_table[4])(double, double) = {
    add, sub, mul, divi};// The first address of the function is stored in the array , Use a function pointer to point to this address to reference this function   To form a function array 

        printf(" Please enter two numbers :");
        scanf("%lf %lf", &x, &y);

        printf(" The result of adding, subtracting, multiplying and dividing these two numbers is :");
        for (i = 0; i < 4; i++)
        {
    
                result = (*func_table[i])(x, y);
                printf("%.2f ", result);
        }
        putchar('\n');

        return 0;
}

double (*func_table[4])(double, double) = {add, sub, mul, divi};
Is an array function , There are four function names ( First address ).
call (*func_table[i])(x, y) It's called [i] The function corresponding to the name .

amount to :

 (*func_table[1])(x, y)==(add(x, y));
 (*func_table[2])(x, y)==(sub(x, y));
 (*func_table[3])(x, y)==(mul(x, y));
 (*func_table[4])(x, y)==(divi(x, y));

General high-level languages have these ,C The language is less used ( But not without ).

A little , Write again when you have time , Relatively complex .

原网站

版权声明
本文为[A egg of maker Association]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101330080105.html

随机推荐