C traps and defects excerpt-semantic "traps"

Source: Internet
Author: User
Tags define null

1.
Pointers and Arrays

 

The arrays in C language are worth noting:

1) in C language, there is only one-dimensional array, and the array size must be determined by the compiler as a constant. The elements of arrays in C language can be any type, including another array. Therefore, we can simulate a multi-dimensional array.

2) For an array, we can only do two things: determine the size of the array and obtain the pointer to the element whose subscript is 0. Other operations, even if it seems to be performed with array subscript, are actually performed through pointers. In other words, any array subscript operation is equivalent to a corresponding pointer operation.

 

If the array name is used to replace the pointer where the pointer should appear, the array name will be treated as a pointer to the element with the subscript of the array 0.

 

For an array named a (such as a in int A [3]), except for the sizeof parameter, in all other cases, a indicates the pointer to the element marked as 0 in array. As we expected, sizeof (a) is the size of the entire array A, rather than the size of the pointer to the element in.

 

* (A + I) is the reference of the Elements marked as I in array A. It is often abbreviated as a [I]. Because a + I and I + A have the same meaning, A [I] And I [a] have the same meaning.

 

For two-dimensional arrays, int Cal [12] [31]; Cal is an array with 12 array elements, each element of its array type is an array with 31 integer elements.

Int * P;

P = Cal;

The assignment of P is invalid. Cal is a two-dimensional array, that is, an array. Here, Cal is used to convert Cal into an array pointing to (Cal [0]). and P is a pointer to an integer variable. "P = Cal" tries to assign a pointer of A type to another type.

 

2. array declaration as a parameter

 

In C language, an array cannot be directly transmitted as a function parameter. When an array name is used as a parameter, the array name is immediately converted to a pointer pointing to the first element of the array. Therefore, using arrays as function parameters makes no sense: the C language will automatically replace the array declaration as the corresponding pointer declaration.

 

Note: In other cases, there is no such automatic conversion.


3. The NULL pointer is not a null string.

 

Except the constant 0, an integer is converted into a pointer in C language. The final result depends on the implementation of the compiler. For 0, the compiler ensures that the pointer converted from 0 is not equal to any valid pointer.
For the sake of code docalization, constant 0 is often replaced by a symbol: # define null 0.

 

Note: When 0 is converted to a pointer, the pointer cannot be unbound from the reference (dereference ). 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.

 

4. Order of Values

 

In C, there are only four operators (&, | ,? : And,) There is a specified order of value. Operator & | first evaluates the left-side operand, and then evaluates the right-side operand only when necessary. Operator? : There are three operands: In? In B: C, A is first evaluated, and then the value of B or C is obtained based on the value of. The comma operator first evaluates the left-side operand, then the value is discarded, and then evaluates the right-side operand.

 

The order in which any other operator in the C language evaluates its operands is undefined. In particular, the value assignment operator does not guarantee any order of value.

 

5. Integer Overflow

 

There are two types of integer arithmetic operations in C language: signed and unsigned. In the unsigned arithmetic operation, there is no "overflow": All the unsigned operations are modeled on the Npower of 2, where N is the number of digits in the result. If one operand of an arithmetic operator is a signed integer and the other is an unsigned number, the number is converted to an unsigned number, and it is impossible to "overflow ". However, when both operands are signed integers, "overflow" may occur and the result of "overflow" is undefined. When the result of an operation "overflows", it is insecure to make any assumptions.

 

 



 

 

 

 

 

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.