(* (void (*) ()) 0) ()

xiaoxiao2021-03-06  14

Overview

In many cases, especially when reading the code, the understanding of the C language states is very important, and the C-language itself concludes simplicity also makes the statement of C language often feel very confused. Therefore, Here I use an article to focus on this problem.

Problem: Declaration and Function

A program stores in a period of starting address 0, if we want to call this program, how do you do it?

answer

The answer is (* (void (*) ()) 0) (). It is really a big, well, let us know, from two different ways to analyze this problem in detail.

Answer analysis: From the end to the head, first, the most basic function declaration: void function (paramlist);

The most basic function call: function (paramlist);

In view of the function of the function in the problem, the function call can be simplified to function ();

Second, according to the problem description, you can know that 0 is the entry address of this function, that is, 0 is a function of a function. The function declaration form using the function pointer is: void (* pfunction) (), the corresponding call form is: (* pfunction) (), the function call in the problem can be written: (* 0) ().

Third, everyone knows that the function pointer variable cannot be a constant, so 0 in the above formula must be converted into a function pointer.

Let's take a look at the function of using the function pointer: such as a void (* pfunction), what is the prototype of the function pointer variable? This problem is very simple, the PFunction function pointer is (void (*)), that is, the variable name is removed, and the whole, the whole () number.

Therefore, the 0 force converted to a return value to a Void, the parameter is empty, the pointer is as follows: (Void (*) ()).

OK, combined 2) and 3) analysis, the result is: (* (*)) 0) ().

Answer Analysis: Understand the answer from head to tail

(Void (*) ()) is a function pointer of a return value of Void and an empty parameter.

(Void (*) ()) 0, convert 0 into a return value as a VOID, the parameter is an empty function pointer, the address pointed to 0.

* (void (*) ()) 0, front plus * Represents the name of the function that is a function of returning value Void

(* (void (*) ()) 0) (), is of course a function.

We can use TypeDef clearly declared as follows:

TYPEDEF VOID (* PFUN) ();

This makes the function becomes (* (PFUN) 0) ();

Question: Analysis of Three Statements

Analysis of the declaration, the most fundamental method is also a class-specific, simplified, simplified, simplifying, and simplifying the three examples by analyzing three examples, by analyzing three examples.

# 1: int * (* a [5]) (int, char *);

First see the identifier A, "[]" priority is greater than "*", A and "[5]" first combined. So A is an array. This array has 5 elements. Each element is a pointer, pointer pointing "(int, char *)", which is obvious, pointing to a function, this function parameter is "int, char * ", Return value is" int * ". OK, ending one. :) # 2: void (* b [10]) (void (*));

B is an array. There are 10 elements of this array. Each element is a pointer. The pointer points to a function, the function parameters are "void (*) ()" [Note 10], the return value is "void". complete!

Note: This parameter is a pointer, pointing to a function, the function parameter is empty, the return value is "void".

# 3. Doube (*) () (* pa) [9];

PA is a pointer, pointer points to an array, with 9 elements of this array, each element is "DOUBE (*) ()" (ie a function pointer, pointing to a function, the parameter of this function is empty, return value Is "double").

转载请注明原文地址:https://www.9cbs.com/read-49041.html

New Post(0)