Why not be proficient in C? 03 in-depth analysis statement

Source: Internet
Author: User
Tags all definition

For complex C function declarations or declarations after the typedef alias, many people are confused. This article mainly analyzes the principles observed in the declaration process in C language.

Statement

Reference chapter 3 of "C expert programming" to describe the declared priority rules:

However, I think this rule is not popular enough. I read the section on const in C ++ Annotation and explained in detail this rule. It is efficient and can be analyzed as follows:

char* const *(*next)();

It means starting from the variable name, hitting) or ending the read to the left, hitting (reading to the right.

And

To what type

Read, encountered, (, the explanation is that a pointer continues to read to the left, (* encountered, the explanation points to a function, the function has no form parameter,
(*, The interpreted function returns a pointer. After interpreting the form parameter table and return value of the function, * (* next) () first encounters const, then *, and finally char, the return value (pointer) of the interpreted function points to a char-type pointer constant, that is, the pointer cannot be assigned a new value, and the value indicated can be changed. To the beginning and end.

The preceding example is an example of function declaration.

The same is true for other statements, such as int * ap [] in the previous article. We also explain this as follows:

  • Read from ap. When I encounter [], it indicates that it is an array.
  • Then, at the end, * indicates that the array element is a pointer,
  • Int indicates that the pointer type of this element is int *

The conventional const int * p; can also be used as follows:

  • Start from p, end to fold, and go to the left.
  • * Indicates a pointer.
  • When int, const, and const are modified as int, this pointer points to a const int.

Similarly, for int * const p: when we look to the left, we first see const and then *, which indicates that the const is modified *.

Come to a complex challenge:

** (*) The ip address is a function pointer, which is an invisible parameter of the function (* This pointer points to an array * this constant Pointer Points to an array

However, we generally do not have such statements to dislike us .. We can basically know how to play it.

Typedef

After the statement is analyzed, typedef should be mentioned in general. This is a major component of C.

First, we need to make it clear that typedef creates an alias for the type, rather than creating a new type.

When talking about typedef, it must be differentiated from # define. define is only a simple macro extension.

There are the following differences:

  • First, the macro type name can be used for extension for other type specifiers, but not for typedef definition.
 INT intunsigned tInt tci;  
  • Secondly, in the continuous variable declaration, typedef can ensure that all definition types are consistent, while # define cannot.
 pD int**pT c,d;   

Be a good friend with typedef

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

Let's analyze what this is:

Signal (...): signal is a function with complex form parameters.

* Signal (...): returns a pointer.

: This pointer points to a function. The parameter of this function is int. the return value of this function is void.

Let's take a look at the signal form parameter table: two parameters, one parameter is int, And the other parameter is a function pointer. This function has an int form parameter and returns void;

In comparison, we can see that the return value of signal is the same as that of func, And it is void (*) (int). However, if this method is used, it is hard to understand it. At this time, our good friend typedef appeared:

typedef (*ptr_to_func)(, ptr_to_func){.....}

In addition, typedef is often used with struct.

typedef  my_struct{....} NewName;

In this way, NewName is equivalent to struct my_struct, with less characters than struct.

Differences between extensions and definitions:

In C, objects can be declared.

Definition: it can only appear in one place and is determined to allocate memory at the same time. It is a special declaration.

Declaration: only objects created elsewhere. Has an extern prefix, acting on the variable

For more information about the specific extension differences, see the subsequent blog posts on pointer and array interpretation.

END

Key point: how to play table tennis? Typedef is an alias.

Related Article

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.