C language function pointer

The method has two function calls are as follows:

void Fun(void)
{
    //do something
}

int main(void)
{
   ...
   Fun();
   ...
   return 0;
}
void Fun(void)
{
  //do something
}

int main(void)
{
   ...
   void (*p)(void);
   p=Fun;
   p();    
   return 0;
}

The above two methods can call the function, the second most in the way of using the callback function, especially in task switching operating systems. So we have to focus on the analysis under the second approach.

The second way to declare a function pointer p, then why write void (* p) (void)? Why does not directly write * p, not to say that the function name is the entry address of the function, why not direct it to the address to the pointer, then it is easy to understand and convenient to save trouble, in fact, think of truth, but in C this language has variable type stuff, such as int assigned to char, the compiler will report an error, the same way there are also a function of the type, the type of function has since then, how do we know what type of variable. Now write a program that follows, note that the program has an error, the error was intentional written, the purpose is to let the compiler error, use the compiler is gcc.

#include "stdio.h"

void Fun(void)
{
    printf("hello world");    
}

int main(void)
{
    void *p;
        p=Fun;
    return 0;
}

Here is the compiler output:

g++.exe -x c++ -c C:\Users\Administrator.SC-201903191934\Desktop\unknown1.c -o C:\Users\Administrator.SC-201903191934\Desktop\unknown1.o -Wall -fpermissive -Wno-sign-compare -g
C:\Users\Administrator.SC-201903191934\Desktop\unknown1.c: In function `int main()':
C:\Users\Administrator.SC-201903191934\Desktop\unknown1.c:12: warning: invalid conversion from `void (*)()' to `void*'

The compiler error mean not speak void (*) () into a void * type type. The warning describes the function of type Fun * () () type, and declare the pointer type is * p, two types of inconsistencies, it led to his mistake, If so, then how to apply for a pointer of type (*) ( ) it? C language function pointer gave this type, namely (*) () type, so in order to make an address pointer to a function, just declare a (*) () type can be.

as follows:

#include "stdio.h"

void Fun(void)
{
    printf("hello world"); 

}

int main(void)
{
void (*p)(void); //函数指针
p=Fun; return 0;
}

In this way it will function Fun address to the pointer p, p is not so much a function pointer, you might as well say that p is a function name, but there is nothing inside the function, where you can be seen from the old function stated.

So we want to run this so-called function p, and only need to use the same function on the line, p ();

Now a look at the code:

void *Fun(void)
{
    printf("hello world");    
    return NULL;
}

Fun function here instead of the function that returns a value, its return value is a pointer type void *, then how do we declare a pointer refers to the function?

Analysis can be seen from the facade, the type of the function should be * (*) (); So now we need to declare a pointer to the same type, i.e., void * (* p) (void); thus only p = Fun ; it can achieve the purpose.

#include "stdio.h"

void *Fun(void)
{
    printf("hello world");    
    return NULL;
}

int main(void)
{
    void *(*p)(void);
    p=Fun;
    return 0;
}

Summary: 1. function name is typed, so the C language, the type matching principle must be strictly observed.

           2. The type of the function name (*) () type, if the return value, the return type can be increased at the top, e.g.

               int * * Fun (int, int); as a function of the type ** (*) (int, int), the corresponding function pointer int ** (* p) (int, int);

Guess you like

Origin www.cnblogs.com/listenscience/p/10991250.html