Home  >  Article  >  Computer Tutorials  >  Implementation of C language functions written using pointers

Implementation of C language functions written using pointers

PHPz
PHPzforward
2024-01-24 21:39:05996browse

Implementation of C language functions written using pointers

Write a function in C language and use pointer method to implement the function

int find(char* source/*source string*/, char* target/*substring*/)//The position returned in the source string is found. If not found, -1 is returned. If it is found, 1 is returned. , change return i to return 1;

{

int i,j;

int s_len=strlen(source);

int t_len=strlen(target);

if(t_len>s_len)

{

return -1;

}

for(i=0;i

{

j=0;

int flag=1;

if(source[i]==target[j])

{

int k,p=i;

for(k=0;k {

if(source[p]==target[j])

{

p ;

j ;

continue;

}

else

{

flag=0;

break;

}

}

}

else

{

continue;

}

if(flag==1)

{

return i;

}

}

return -1;

}

Programming using pointers to functions

Function pointers are sometimes very useful. For example, by defining a function pointer in a structure, you can implement features similar to C class member functions in C language.

The following program defines an array of function pointers. According to the remainder of the input number (1 or 0), the corresponding function can be called without if else.

//The input is an odd number

void FuncOdd(int n)

{

float i, nIterator = 0;

printf("You entered an odd number.\n");

for (i = 1; i {

nIterator =1/i;

}

printf("And the result is: %f\n",nIterator);

}

//Input is an even number

void FuncEven(int n)

{

float i, nIterator = 0;

printf("You entered an even number;\n");

for (i = 2; i {

nIterator =1/i;

}

printf("And the result is: %f\n",nIterator);

}

int main()

{

int nInput = 0; //Storage the entered number

void (*func[2])(int); // Define function pointers, pointing to two different functions

func[0] = FuncEven;

func[1] = FuncOdd;

printf("Please input a number:"); // Get input from the console

scanf("%d",&nInput);

(*func[nInput%2])(nInput); // Call the corresponding function based on the input number

return 0;

}

C language pointer usage

int top should not belong to the category of pointers, but the definition of common variables of int. This top depends on how it is used. If it is used according to negative numbers, 0, and positive numbers, it can be judged whether it is used as A special definition identifier of this class; if this variable is a counter in a loop (meaning that as the number of loops increases or decreases), you can determine how many times the loop has been successfully executed.

Reader is a class name, which means that the class is also a data type

Just like int, it is a data type name

Reader read[Maxr]; means that an array variable of this type is defined. Marxr is probably a value defined by a macro

Why can it be defined like this? It should be because class can contain any data type (the same as struct in c), including external classes

The above is the detailed content of Implementation of C language functions written using pointers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete