C language function pointer code implementation function, C language function pointer
Preface
Among the many advanced programming languages, the C language is the most close to the underlying layer. You can program the hardware and perform system-level development. This is incomparable to other languages, why is C language powerful? The main source pointer is the core feature of C language. It is also a hurdle for many beginners to learn C language, therefore, many other advanced programming languages directly wrap pointers to reference them. As the saying goes, C is born of everything. Many other advanced languages have evolved from C. However, we must learn from difficulties and make it easy to use. If you do not have a deep understanding of the pointer, do not master the C language. Then the function pointer is the most powerful part of the pointer. With it, we can easily program functions and pass functions as parameters, which can easily implement callback functions! The significance and benefits of the callback function will not be discussed here. The following uses function pointers to implement some common functions.
Code Implementation
Today, function pointers are used to sort and print all one-dimensional arrays. You need to provide callback functions for element operations, that is, function pointers.
Function pointer Introduction
A function pointer is a pointer variable pointing to a function.. Therefore, the "function pointer" should be a pointer variable first, but the pointer variable points to the function. This is just like using pointer variables to point to integer variables, struct variables, and arrays. Here we point to functions. As mentioned above, during C compilation, each function has an entry address, which is the address pointed to by the function pointer. With the pointer variable pointing to the function, you can use the pointer variable to call the function, just as you can use the pointer variable to reference other types of variables. These concepts are basically the same.Function pointers have two purposes: Calling functions and making function parameters..
Code Implementation
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void fun03_intPrint (void * a)
{
int * num = (int *) a;
printf ("% d", * num);
return;
}
int fun03_intCompare (void * a, void * b)
{
int * num1 = (int *) a;
int * num2 = (int *) b;
return * num1- * num2;
}
void fun03_intSwap (void * a, void * b)
{
int * num1 = (int *) a;
int * num2 = (int *) b;
int temp = * num1;
* num1 = * num2;
* num2 = temp;
return;
}
/ *
Features:
Print all elements of an array
parameter:
arr array first element address
eleSize array element size, determines the element pointer step size
length array elements
callback function to print each element
* /
void fun03_printAllArr (void * arr, int eleSize, int length, void (* print) (void *))
{
char * ch = (char *) arr;
for (size_t i = 0; i <length; i ++)
{
print (ch + i * eleSize);
}
printf ("\ n");
}
/ *
Features:
Sort all types of one-dimensional arrays
parameter:
arr array first element address
eleSize array element size, determines the element pointer step size
length array elements
compare collation callback function
swap data exchange callback function
* /
void fun03_chooseSortAllArr (void * arr, int eleSize, int length, int (* compare) (void *, void *), void (* swap) (void *, void *))
{
char * ch = (char *) arr;
for (size_t i = 0; i <length-1; i ++)
{
int index = i;
for (size_t j = i + 1; j <length; j ++)
{
int result = compare (ch + i * eleSize, ch + j * eleSize);
// The former is large, ascending
if (result> 0)
{
index = j;
}
}
if (index! = i)
{
swap (ch + i * eleSize, ch + index * eleSize);
}
}
return;
}
void fun03_test ()
{
int arr [] = {1,2,4,8,12,11,6};
printf ("Before sorting:");
fun03_printAllArr (arr, sizeof (int), sizeof (arr) / sizeof (int), fun03_intPrint);
fun03_chooseSortAllArr (arr, sizeof (int), sizeof (arr) / sizeof (int), fun03_intCompare, fun03_intSwap);
printf ("After sorting:");
fun03_printAllArr (arr, sizeof (int), sizeof (arr) / sizeof (int), fun03_intPrint);
return;
}
int main (int argc, char * argv [])
{
fun03_test ();
return 0;
}
Code running result