Statement syntax in C Language

Source: Internet
Author: User

Overview

In many cases, especially when reading data written by othersCodeThe ability to understand C language statements has become very important, and the concise and concise C language also makes C language statements often confusing. Therefore, here I will use an article to focus on this issue.

Question: Declarations and functions

A program is stored in a memory segment with the starting address 0. If we want to call this program, what should we do?

Answer

The answer is (* (void (*) () 0 )(). It looks really big. Well, let's take two different approaches to thoroughly analyze this problem.

Answer analysis: from end to end

First, the most basic function declaration: void function (paramlist );

The most basic function call: function (paramlist );

Given that the function in question does not have parameters, function calling can be simplified to function ();

Secondly, according to the Problem description, we can know that 0 is the entry address of the function, that is, 0 is a pointer to the function. The function declaration form using the function pointer is: void (* pfunction) (), the corresponding call form is: (* pfunction )(), function calls in the question can be written as follows: (* 0 )().

Third, we know that the function pointer variable cannot be a constant, so the 0 in the above formula must be converted to a function pointer.

Let's take a look at the prototype of function pointer variables for functions using function pointers, such as void (* pfunction? This problem is very simple. The pfunction function pointer prototype is (void (*) (), that is, remove the variable name. For clarity, add the () number.

Therefore, convert 0 to void. The function pointer with the null parameter is as follows: (void (*)()).

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

Answer analysis: Understanding the answer from start to end

(Void (*) () is a function pointer prototype with void returned and null parameters.
(Void (*) () 0. Convert 0 to a function pointer with the return value void and null parameter. The Pointer Points to 0.
* (Void (*) () 0. Add * above to indicate the name of a function whose return value is void.
(* (Void (*) () 0) (), which is of course a function.

We can use typedef to clearly declare the following::

Typedef void (* pfun )();

In this way, the function is changed to (* (pfun) 0 )();

Problem: analysis of the three statements

The most fundamental method for analyzing statements is the analogy replacement method. The analogy, simplification, and understanding are made from the most basic statements. The following three examples are analyzed, to illustrate how to use this method.

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

First, we can see that the identifier name A has a higher priority than "*". A and "[5]" are combined first. So a is an array. This array has five elements. Each element is a pointer and the Pointer Points to "(INT, char *)". Obviously, it points to a function, this function parameter is "int, char *", and the return value is "int *". OK. :)

#2: void (* B [10]) (void (*)());

B is an array with 10 elements. Each element is a pointer pointing to a function. The function parameter is "void (*) ()" [Note 10 ], the return value is "Void ". Finished!

Note: this parameter is a pointer pointing to a function. The function parameter is null and the return value is "Void ".

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

Pa is a pointer pointing to an array with nine elements. Each element is "Doube (*) ()" (that is, a function pointer pointing to a function, the parameter of this function is null, And the return value is "double ").

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.