C traps and defects sorting II

Source: Internet
Author: User
Tags define null

1. in C language, we cannot pass an array as a function parameter. If we use an array name as a parameter, at this time, the array name is immediately converted to the pointer to the first element of the array.
This can be further understood. For example, if the defined array is int A [3], a will be converted to the int * type after being passed as a parameter; if the defined array is int A [3] [4], A is changed to int (*) [4] after being passed as a parameter; if the defined array is int A [3] [4] [5], after passing a as a parameter, it will become int (*) [4] [5]; and so on. Why? Because multi-dimensional arrays in C language are simulated using one-dimensional arrays, that is, each element of a one-dimensional array can be another type of data unit, even if this data unit is another array, however, from the above point of view, when one-dimensional array a is passed as a parameter, it will automatically degrade to the pointer pointing to the first unit of the one-dimensional array, therefore, if the first unit is a one-dimensional array, a degrades to a one-dimensional array pointer. If the first unit of A is a two-dimensional array, then a degrades to a two-dimensional array pointer, so the above conclusion is not hard to come up.

2. What is the output of the following code snippet?
Void print (int B [])
{
Printf ("% d", sizeof (B ));
}
Int main (void)
{
Int A [4];
Print ();
Return 0;
}
Analysis: To find out the output of this code snippet, you must also know the array transfer process when calling a function. The first point above has already been mentioned, when passing parameters, the array is automatically degraded to the pointer pointing to its first unit. Therefore, such a value assignment occurs during function transmission, int B [] = A or more clear int B [] = & A [0], but such a statement compiler will think it is a wrong syntax! However, in practice, it is often possible that we will use this method, but there is no error. This is because the compiler will force B to perform a degradation here and degrade it to an int * pointer type. Therefore, the output content of the above program fragment is obvious. The output is the size of a pointer variable of the int type, that is, 4 (32-bit system ).

3. Two main function parameters
Int main (INT argc, char * argv [])
Int main (INT argc, char ** argv)
Note that the previous statement emphasizes that argv is a pointer to the starting element of an array. The elements of this array are character pointers.

4. the following statement:
Char * P = "XYZ ";
P [0] = 'a ';
The compiled device may not have any problems, but it is likely to encounter a prompt similar to a certain address that cannot be written. K & rc describes the modification behavior as follows: the attempt to modify a String constant is undefined. Although some compilers allow such behaviors, such writing is not worth advocating.

5. Except for an important exception, an integer is converted into a pointer in C language. The final result depends on the implementation of the C compiler. In this special case, the constant 0 is used. The Compiler ensures that the pointer converted from 0 is not equal to any valid pointer. For the sake of code documentation, the constant 0 value is often replaced by a symbol:
# Define null 0
It should be noted that when constant 0 is converted to a pointer, this pointer cannot be removed from the reference (removing the reference is to use (* P) such operations to retrieve the content in this address ), in other words, when we assign 0 to a pointer variable, we absolutely cannot attempt to use the content stored in the memory to which the Pointer Points.

6. Benefits of "asymmetric boundaries" in C Language
After defining an array int A [10] in C language, the subscript of the array is 0 ~ 9 is a valid subscript, and 10 is beyond the range of the array. What are the benefits of doing so?
For the first benefit, see the following example:
For (I = 0; I <10; I ++)
A [I] = * P ++;
If you require operations on the cells after the range of begin (0) and end (10) is given, if the given begin and end are the same, this method can avoid errors. At the same time, the number of units to be operated can be simply obtained through the end-begin. The premise is that the user's begin and end both comply with the "asymmetric boundary" usage method of the C language. If asymmetric boundary is not used (at this time, the subscript of the array is 1 ~ 10 valid) such as code:
For (I = 1; I <= 10; I ++)
A [I] = * P ++;
To initialize or traverse the array. After writing, the number of actually operated units is 10-1 + 1 = 10, if a programmer forgets to add 1 during programming, such a computing process may easily cause bugs in the program. At the same time, if you replace 1 and 10 with the begin and end variables, the begin and end values passed by the user when calling this function are the same value, this code will also operate on the [begin] value in the array, which will also cause difficulties for callers.
The second advantage is that we can use & A [10] as a judgment condition as a sign of the completion of buffer or Array Operations, which is quite convenient in actual programming. Although the operation on the value of a [10] is illegal, it is specified in ANSI that the operation of & A [10] is legal.

7. in most C language implementations, -- N> = 0 is at least as fast as the equivalent n --> 0, and even faster in some C implementations, the calculation of the first expression -- N> = 0 is to first subtract 1 from N and then compare the result with 0. The calculation of the second expression first saves n, then subtract 1 from N and compare the size of the saved value with 0.

8. There are only four operators in C Language (&, | ,? :,) There is a specified order of value, operator & and operator | First, evaluate the left-side operand. Only when necessary will the right-side operand be evaluated. Operator? : There are three operands in? In B: C, operand A is first evaluated, and the value of operand B or C is evaluated based on the value of a (only one expression is calculated for B and C ). The comma operator first evaluates the left-side operand, and then the value is discarded. In the right-side operand, the value of the entire expression is the value of the rightmost expression.
Example of the comma OPERATOR: A = (1, 2, 3 );
A is finally assigned 3 values.
Note: The comma used to separate function parameters is not a comma operator. For example, the order of values in F (x, y) is undefined, while in function g (x, y )) in the latter example, function g has only one parameter. The value of this parameter is the value of the comma operator in parentheses.
Note: In C, the order in which all other operators evaluate their operands is undefined. In particular, the value assignment operator does not guarantee any order of value. If you use the same variable for multiple times in an expression, the Operation consequences such as ++ or -- are sometimes unpredictable. For example:
Y [I] = x [I ++];

9. The result of a logical operator is a logical value, that is, true (1) or false (0). In logical judgment, 0 is usually regarded as false, not true. So! The value of the 10 expression is false (0), because the value of the 10 non-0 expression is regarded as true during non-computation and true or false.

10. There are two types of integer arithmetic operations in C language, including the number of signed and unsigned numbers. There is no overflow statement in the number of unsigned operations. However, the number of signed operations may overflow. When the result of an operation is "overflow, it is insecure to make any assumptions. In case of overflow, the two operands A and B should be forcibly converted to an unsigned integer:
If (unsigned) A + (unsigned) B> int_max)
Complain ();
Int_max is a defined constant, which represents the maximum possible integer. The ansi c standard defines int_max in <limits. h>. If it is implemented in other C languages, you may need to redefine the value yourself.

C traps and defects sorting II

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.