C language function and pointer application summary

  • 2020-04-01 23:31:50
  • OfStack

1. First of all, in C language, function is a function-to-pointer method, that is, for a function, it will be automatically converted to the pointer type.


#include<stdio.h>
void fun()
{
}
int main(void)
{
   printf("%p %p %pn", &fun, fun, *fun);
   return 0;
}

-------------------------------------------------------------------------------------------

The result of these three values is the same. In fact, for the last *fun, even if I put many * signs in front of it, the result is the same, that is, the result of **fun, ***fun is the same.
To this question, because before to deliver function is a way of function - to - pointer, it will automatically be converted to a pointer type, & fun is the address of the function, as the pointer type, fun is a function that will convert the pointer type, and for fun, because the fun has become a pointer type, pointing to the function, so * fun is the address of the function, and according to the function - to - pointer, the function is turned into a pointer, so and so on, These three values are the same.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2. How to call a function at an address
If you know the address of a function, you can force it into a certain type of function pointer, and then according to the pointer to call the function of this address, such as:


#include<stdio.h>
void f(int i)
{
   printf("i = %dn", i);
}
int main(void)
{
   unsigned long add;
   add = (unsigned long)f;
   ((void (*)(int))add)(10);
   (*(void (*)(int))add)(20);
   return 0;
}

---------------------------------------------------------------------------------------
The (void (*)(int) method converts an address to a pointer type of a function with an int argument and no return value, and then calls it.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

3. Function pointer array usage.
Sometimes you need to define an array whose contents are a series of function Pointers, and then call it, such as:

#include<stdio.h>
int max(int v1, int v2)
{
   return (v1 > v2 ? v1 : v2);
}
int min(int v1, int v2)
{
   return (v1 < v2 ? v1 : v2);
}
int sum(int v1, int v2)
{
   return (v1 + v2);
}


int main(void)
{
   int (*p[3])(int, int);
   p[0] = max;
   p[1] = min;
   p[2] = sum;
   printf("p[0] = %dn", (p[0])(3, 5));
   printf("p[1] = %dn", (p[1])(4, 6));
   printf("p[2] = %dn", (p[2])(1, 2));
   return 0;
}

-----------------------------------------------------------------------------------------
Although this method feels a bit cumbersome, but it is also a way to use, so introduce.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Returns a pointer to an array

Function returns a pointer to an array, such as:


#include<stdio.h>
#include<stdlib.h>
int (*p())[10]
{
   int (*m)[10];
   int i;
   m = (int (*)[10])malloc(10 * sizeof(int));
   if (m == NULL) {
      printf("malloc errorn");
      exit(1);
   }
   for (i = 0; i < 10; i++)
      *(*m+i) = i+1;

   return m;
}


int main(void)
{
   int (*a)[10];
   int i;
   a = p();
   for (i = 0; i < 10; i++)
      printf("%d ", *(*a+i));
   printf("ndonen");
   return 0;
}

-------------------------------------------------------------------
In this way,int (*a)[10] is a pointer to a one-dimensional array, and p() returns a pointer to a one-dimensional array.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Returns a pointer to a function pointer

/ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = /
/ see the example of quicksort using the function that returns a pointer. /
/ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = /

The signal() function is the best example of this problem.
Void (*signal (int signo, void (*func)(int)))(int);
Many of you didn't understand the definition of this function when you first read it, but you can actually read it step by step.
Int (* p) ();
This is a function pointer to a function that takes no arguments and returns an int.
Int (* fun () ();
This formula and the formula of the difference is that with the fun () instead of the p, and fun () is a function, so it can be seen as fun () function, it is the return value of a function pointer, the function pointer (p) is the above points to the function is a without any arguments, and a function return value as an int.

So signal() can be thought of as a signal() function (which itself takes two arguments, an integer and a pointer to a function), and the signal() function returns a pointer to a function that takes an integer and returns a void.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The signal function returns a pointer to a previous signal handler, so here's an example of returning a pointer to a function.


#include<signal.h>
#include<stdlib.h>
#include<stdio.h>
void sig_fun2(int signo)
{
   printf("in sig_fun2:%dn", signo);
}
void sig_fun1(int signo)
{
   printf("in sig_fun1:%dn", signo);
}
int main(void)
{
   unsigned long i;
   if (signal(SIGUSR1, sig_fun1) == SIG_ERR) {
      printf("signal fun1 errorn");
      exit(1);
   }
   (signal(SIGUSR1, sig_fun2))(30);
   printf("donen");
   return 0;
}

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
6. The use of function Pointers as arguments (mentioned in the previous record).
In the argument to a function, there may be a function pointer, which occurs in the signal() function.
In fact, in many sort functions, this parameter is used as the function pointer to make the call, such as Quicksort

Such as:


#include<stdio.h>
int max(int v1, int v2)
{
   return (v1 > v2 ? v1 : v2);
}
int min(int v1, int v2)
{
   return (v1 < v2 ? v1 : v2);
}
int sum(int v1, int v2)
{
   return (v1 + v2);
}
int fun(int a, int b, int (*call)(int, int))
{
   return (call(a, b));
}
int main(void)
{
   printf("max=%dn", fun(1, 2, max));
   printf("min=%dn", fun(3, 4, min));
   printf("sum=%dn", fun(5, 6, sum));
   return 0;
}


Related articles: