C language understanding (* (void (*)) 0) () of

A, (* (void (*) ) 0) () understood.
Introduction : In some complex code project, often appear similar to (* (void (*)) 0) () This complex expressions, today will take you to slowly dissect this expression, teach us to understand.

1. An essential function name.
The essence of the function name is the first address of a function when performing this function, in fact, jump to the address to execute binary code downloaded at this address, you can understand by the following procedure:

#include <stdio.h>
int (*test)(int);
int Print(int num)
{
    printf("The num is %d\n",num);
}
    int main()
{
    test = Print;       
    (*test)(1);
    printf("The addr is %p\n", test);
    (*(int(*)())0x804841d)(2);
} 

The results are running:

The num is 1 
The addr is 0x804841d
The num is 2

Analysis of the code:

  1. First by int (* test) (int) defines a int (*) function pointer type, then the Print function (essentially an address) into the pointer variable to the test, so when performing (* test) (1) when essentially on the implementation of the Print (1).

  2. Secondly, by directly printing test this pointer variables can be found in the first address of this function is 0x804841d.

  3. Finally, in order to confirm this address is the first address of the function, so we put this address with the cast become int (*) () type function pointer, that is, (int (*) ()) 0x804841d, finally, if you want to refer to this function, it must be a pointer solution function reference, namely (* fuc) (2), and fuc pointer here is (int (*) ()) 0x804841d, so (* (int (*) ()) 0x804841d) ( 2) means that string address these casts become int (*) () function pointer type, and then perform pointer dereference it!

So, to resolve this problem (* (void (*)) 0) () is also above the, (void (*)) 0 essence is to address 0 cast to become void (*) type function pointer, then this pointer dereference, will become a (* (void (*)) 0) () a.

Second, the use of re-naming Typedef complex pointer variable.
1.Typedef rename definitions so that the type of function pointers can be more convenient and easy, and the function pointer rename a rule such as:

typedef int (*pF)(int);

The meaning of this sentence rename a pF int (*) (int) type, so the above code rewrite rename embodiment can be obtained:

#include <stdio.h>
typedef int (*pF)(int);
int Print(int num)
{
    printf("The num is %d\n",num);
}
    int main()
{
    pF test = Print;
    (*test)(1);
    printf("The addr is %p\n", test);
    (*(pF)0x804841d)(2);
}

Resolution:

  1. pF test = Print used later renamed directly pF defined test as int (*) (int) type, and the assignment Print function to the function pointer test to go, while the back (* (int (*) ()) 0x804841d) (2) can be turned into (* (pF) 0x804841d) (2) is also a lot easier than before, due pF type is int (*) (int) type, so just cast this address will become pF type and dereference It can be.

  2. Thus, (* (void (*)) 0) () may be simplified as follows:

     typedef void (*pF)();
    (*(pF)0)();
Published 24 original articles · won praise 27 · views 10000 +

Guess you like

Origin blog.csdn.net/gyyu32g/article/details/86428831