C language collection (III): Analysis of C language statements

Source: Internet
Author: User

What is difficult to understand in C language is pointers, but what is harder to understand than pointers? I don't think it's a C language statement.

For example, let's look at the function declaration of the next system call signal:

void (*signal(int sig, void(*func)(int))) (int);

Is it dizzy? Which identifier does it start to read, singnal? Sig? Or int? Read to the left or read to the right?

In fact, it is easy to understand the priority rules declared in C language.

A declares that it reads data starting from its name and then reads data in order of priority.

B priority from high to low:

B .1 The part of the identifier expanded by parentheses

B .2 suffix OPERATOR: () indicates that this is a function, and [] indicates that this is an array.

B .3 prefix OPERATOR: asterisk * indicates "pointer"

C. If the const and (or) volatile keywords follow the type specifiers (int, long, etc.), then it acts on the type specifiers. Otherwise, let's look forward, the keyword acts on the type character or pointer asterisk next to the left.

Well, the theory is used for practice. Let's look at an example.

char * const *(*next)();

First, read next. The prefix to the left is *, indicating that next is a pointer, followed by (), indicating that next points to the function. The return value of the function is a pointer, And the Pointer Points to char * const, A constant pointer pointing to the character type (the const acts on the pointer ).

Let's look at another example.

char *(* c[10])(int **p)

Analysis: c is an array with 10 elements and pointers. These pointers point to functions. The function parameter is int ** p (pointer to pointer ), the return type of the function is char * (character pointer ).

Now, let's look back at the singal () Statement:

void (*signal(int sig, void(*func)(int))) (int);

Analysis: signal is a function. It is a parameter of the signal function, an int sig, and void (* func) (int), which indicates a function pointer pointing to an int parameter, returns the void function.

The Return Value of the signal function is also a function pointer. It also points to a void function with an int parameter.

This makes it clearer. Like Ding jieniu. At this time, you find that there is no signal, but it is not concise enough. Is it almost the same if you look at the blue font in the analysis?

TryTypedefRight.

typedef void (*ptr_to_func)(int);

A new name is given for a type, which is a function pointer that points to a parameter of int and returns void. In this way, the signal can be declared as follows:

ptr_to_func singal(int sig, ptr_to_func func);

Is it concise? Of course, it can be more concise without considering readability (not recommended)

typedef void (*A)(int);A singal(int sig, A func);

Note:Typedef only gives a new name for a type, rather than declaring a new type.

--- End ---

 

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.