C traps and defects sorting 1. C traps and defects sorting

Source: Internet
Author: User

C traps and defects sorting 1. C traps and defects sorting
1. Greedy method in lexical analysis"
Some symbols of C language, such as/, *, and =, are only one character long and are called Single Character symbols. Other symbols in C language, such as/*, =, and identifiers, contain multiple characters, which are called multi-character symbols. When the C compiler reads a character '/' and then followed by another character '*', the compiler must make a judgment that it is treated as two separate symbols, or use them together as a symbol. The C language's solution to this problem can be summarized into a very simple rule: each symbol should contain as many characters as possible. That is to say, the compiler splits the program into symbols by reading a character from left to right. If the character may constitute a symbol, it will then read the next character, judge whether a string consisting of two characters can be a component of a symbol. If possible, continue to read the next character and repeat the above judgment, it is no longer possible for a string consisting of characters to be read to form a meaningful symbol.
However, except for character strings and character constants, there cannot be blank spaces (space characters, tabs, and line breaks) in the middle of the symbols ). Note the following two examples:
A --- B is equivalent to a ---B
A --- B is not equivalent to a--- B
Y = x/* p cannot achieve the expected purpose. You can change it to y = x/(* p) or y = x/* p.

2. A string enclosed by double quotation marks indicates a pointer pointing to the starting character of an unknown array, the array is initialized by characters between double quotation marks and an extra binary value of 0 '\ 0.

3. the 'yes' character can be processed differently according to the compiler: most compilers understand it as an integer, the integers 'y', 'E', and 'S' are combined according to the methods defined in the specific compiler implementation; borland C ++ v5.5 and RFC v3.6 ignore redundant characters, and the final integer is the integer of the first character; the method used in Visual C ++ 6.0 and GCC v2.95 is to overwrite the previous character with the last character in sequence, and the final integer obtained is the integer of the last character.

4. The declaration of any C variable consists of two parts: type and a set of similar expressions. On the surface, the Declaration is similar to the expression. evaluate it and return the result of a given type in the Declaration.
Float ff ();
The meaning of this statement: The result of the expression ff () is a floating point number, that is, ff is a function that returns a floating point type.
Float * pf;
The meaning of this statement: * pf is a floating point number, that is, pf is a pointer to a floating point number.
Float * g ();
The meaning of this statement: * g () is a floating point number, and g () is equivalent to the above pf. g is a function, and the function value is the return value of the function, therefore, the value returned by g () should be of the same pf type in the above example, that is, a floating point pointer.
Float (* h )();
The meaning of this statement: (* h) () is a floating point number, but (* h) () is a function, so h is a function pointer, the function pointed to by h returns a floating point number.

5. Build a forced type conversion character
Starting from declaring variables, remove the variable name and semicolon at the end of the Declaration in the declaration, and enclose the remaining parts in a bracket.
Float (* h) (); --> (float (*)())
Description of the forced type conversion character after: the pointer of the function that returns the floating point type.
Now let's analyze the meaning of (* (void (*) () 0) ()
Obviously, before the constant 0 is a forced type conversion character (void (*) (), which means that the return value type is a void type function pointer. Therefore, the meaning of the entire expression is to convert 0 to a void function pointer with the return value type. Then, use the * operator to obtain the function pointed to by this 0, and then call it. Using typedef makes this function clearer:
Typedef void (* funcptr )();
(* (Funcptr) 0 )();
A complex example:
Void (* signal (int, void (*) (int );
Meaning: first, we should regard signal (int, void (*) (int) as a whole. In this way, this whole should represent a function pointer, the outer layer calls the function pointed to by this pointer through the * operator. However, signal (int, void (*) (int) itself is better understood. signal is a function, and the first parameter received is an int value, another parameter is a function pointer. However, since this whole represents a function pointer, we mentioned earlier that the function itself represents a value, which is actually the value returned by the function. Therefore, the signal function returns a function pointer, the pointer type is void (*) (int ). So how do I call the signal declared by void (* signal (int, void (*) (int? In fact, the usage is very simple:
Signal (int m, void (* ptr) (int); can be called. The above declaration method is used to make the return value of this function more meaningful, the caller can define a void (*) (int) pointer to receive the return value of the signal function.
This function can also be implemented by typedef and become clearer:
Typedef void (* HANDLER) (int );
HANDLER signal (int, HANDLER );

6. When you need to add a symbol to a variable, you can use the following method:
Int a = 3;
A =-;

7. What is the role of a comma in the C language initialization example table?
Analysis: This is for the convenience of lexical analyzer. After writing, every variable used for initialization ends with a comma, which makes it easier for the compiler to process.

8. after defining an array a, except as a parameter of the sizeof operator, in all other cases, a represents the pointer of the element marked as 0 in, this applies to multi-dimensional arrays, because in C, multi-dimensional arrays are simulated by one-dimensional arrays, except that each element of one-dimensional arrays can be other types of variables, it can also be another array. So the following example:
Int a [3];
* A = 2;
* (A + 1) = 3;
These operations are performed on 0th and 1st elements in array a, but these operations are often troublesome, therefore, a [0], a [1], and a [2] are commonly used in the abbreviated form above. * (A + 1) is equivalent to * (1 + a), while * (a + 1) is abbreviated as a [1], and * (1 +) the abbreviation is 1 [a], so a [1] is equivalent to 1 [a]. Many compilers do not report errors, but the second method is not recommended !!

9. What is the output in the following example?
Int a [2] [3];
Printf ("% d, % d", sizeof (a + 1), sizeof (a [1]);
Analysis: first, we will analyze the output content step by step. When the sizeof (a + 1) compiler encounters this sentence, a will be treated as a pointer, Because array + 1 is meaningless, and only the pointer + 1 of the array makes sense, so what we test here is still the size of a pointer, if it is a 32-bit PC, the output will be 4. Next, we will analyze sizeof (a [1]). This test shows the size of the 2nd units in the one-dimensional array a. What is stored in the second unit? The second unit contains an array of three units, so the value tested by this expression is 12.
Answer: 4, 12

10. When the malloc () function fails to allocate memory, a null pointer is returned. When using malloc, you must determine whether the returned value is a null pointer. Otherwise, unexpected consequences will occur.
Q: Where should I start from "C traps and defects", "Data Structure", "C expert programming", and "C and pointer?

In my opinion, the sequence should be "c and pointer", "Data Structure", "c expert programming", and "C traps and defects". Many data structures are based on operations on pointers, the data structure is very good, there are a lot of algorithms that can run more efficiently, this book is very important, "c expert programming" has not read this book, C traps and defects is a book that makes a qualitative change to c's understanding, such as how to understand (* (void (*) () 0) () and so on, hope to help the landlord
We are studying Tsinghua. I feel pretty good. It's a good book.

What's the next project

Welcome! Welcome! Welcome! Welcome! Welcome! Welcome!
 

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.