"C Traps and Defects" Study Notes--Chapter 2

2.1 Understanding function declarations

First distinguish float *g(), (*h)();
    float *g(); g is a pointer whose return value type is a pointer to a floating point number
    float (*h)(); h is a function pointer , and the return value is floating point

Analysis: ((void(*)())0)();

Simplify with typedef (*(void(*)())0)()
typedef void (*tmp)();
(*(tmp)0)(); ///Here 0 represents the process or function pointing to 0 address

For (*(tmp)0)(); where tmp stands for cast type, because 0 cannot be used as a function pointer
Declare a function pointer: typedef void (*tmp)();
By declaring like this, the result after the action of * can be called as a function

2.2 Operator precedence issues


Obviously the assignment operator has less precedence than the ternary operator

2.3 Note the semicolon as the end of the statement

Pay attention to whether the semicolon is written more or less after if, else, while, structure, and return

2.4 switch statement

switch(t){
    case 1: statement; break;
    case 2: statement; break;
    case 3: statement; break;
    default;
}

Note that there is a break after each statement, as well as default. Of course, if there are special circumstances, it can be omitted, and a comment should be made at that time.

2.5 Function call

C language requirements: When the function is called, even if the function does not take parameters, it should also contain a parameter list

f(); is the function call statement

f ; this statement computes the address of the function f without calling the function

2.6 Problems caused by "hanging" else

else is always combined with the nearest unmatched if within the same pair of parentheses


practise:

2-1 C language allows redundant commas in the initialization list. What are the features?

In this way, the list can be processed more conveniently when the compiler processes it.

For example, when we usually do acm questions, there are multiple outputs in one line, there is a space between every two numbers, and finally a new line

If you can output a space after the last number here, can you reduce the workload? The same principle applies to this question

2-2 C language has brought many problems as the delimiter of statements, so what is the method of changing to delimiter statements in other languages? Are there any defects

For python, each line is used as a statement, which can be understood as a newline as a statement separator

What about such a defect: it is impossible to write multiple statements in one line to reduce the number of lines of code and make the program more streamlined


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324845134&siteId=291194637