The syntax of the declaration of C language

Source: Internet
Author: User

Overview

In many cases, especially when reading other people's written code, the ability to understand the C language statement becomes very important, and the C language itself concise also makes the C language statement is often very confusing, so here I use a content to focus on this issue.

Issues: Declarations and functions

A program is stored in a section of memory starting at 0, and if we want to call this program, how do I do it?

Answer

The answer is (* (void (*) ()) 0) (). It looks really big, that's good, let's be hard to understand, from two different ways to analyze the problem in detail.

Answer analysis: From the end of the head

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

The most basic function call: Functions (Paramlist);

Given that the function in the problem has no parameters, the function call can be simplified to functions ();

Secondly, according to the description of the problem, you can know that 0 is the entry address of this function, that is, 0 is a pointer to a function. function declarations using function pointers are: void (*pfunction) (), and the corresponding invocation form is: (*pfunction) (), the function call in the question can be written: (*0) ().

Third, as you know, a function pointer variable cannot be a constant, so 0 of the upper formula must be converted to a function pointer.

Let's take a look at a function that uses a function pointer: void (*pfunction) (), what is the prototype of the function pointer variable? The problem is simple: the Pfunction function pointer prototype is (void (*) ()), which is to remove the variable name, and for clarity, the whole plus () number.

So casting 0 to a return value of void with null arguments is the following: (Void (*) ()).

OK, combine 2 and 3), the results come out, that is: (* (Void (*) ()) 0) ().

Answer Analysis: Understand the answer from beginning to end

(Void (*) ()) is a function pointer prototype with a return value of void and an empty argument.
(Void (*) ()) 0, convert 0 to a function pointer with a return value of void, a null argument, and a pointer to an address of 0.
* (void (*) ()) 0, preceded by * means the name of a function whose return value is void
(* (Void (*) ()) 0) (), which of course is a function.

We can use the typedef to clearly declare the following:

typedef void (*pfun) ();

This function becomes (* (pfun) 0) ();

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.