"C Traps and defects" finishing two

Source: Internet
Author: User
Tags define null

1. Array Masterpieces
In the C language, we have no way to pass an array as a function parameter, assuming we use the array name as the parameter. At this point the array name is immediately converted to a pointer to the first element of the array.
The understanding of this can go one step further, for example, the definition of an array of int a[3], then a as a parameter is passed into an int * type; if the defined array is int a[3][4], then a is passed to Int (*) [4] as the number of the parameters. Assume that the defined array is int a[3][4][5]. Then a as a parameter is passed into an int (*) [4][5]; Why is this possible? Since multidimensional arrays in the C language are emulated by one-dimensional arrays, each element of a one-dimensional array can be another type of data unit. Even if the data unit has an array, however, according to the above view, a one-dimensional array A will be actively returned to a pointer to the first cell of that one-dimensional array when it is passed as a parameter, so if the first cell is a one-dimensional array, A is degenerate into a one-dimensional array pointer. Suppose that the first cell of a is a two-dimensional array. Then a is degenerate into a two-dimensional array pointer, so the above conclusion is not difficult to draw.

2. What is the output of the code snippet below?
void print (int b[])
{
printf ("%d", sizeof (b));
}
int main (void)
{
int a[4];
Print (a);
return 0;
}
Analysis: To figure out the output of this piece of code. It is also clear that the process of passing the array at the time of the function call. The 1th above has already been said. The array has itself been degraded as a pointer to its first cell at the time of passing the reference, so the process of passing the function is equivalent to the occurrence of such an assignment, int b[] = A or more clearly some int b[] = &a[0], But this kind of statement compiler will feel is a wrong syntax! However, in practice we may often use this to do so without error, because the compiler here will force B to degenerate, degenerate into an int * pointer type. So the above program fragment output is obvious, the output is an int type pointer variable size, that is, 4 (32-bit system).

Two types of 3.main function parameters
int main (int argc, char *argv[])
int main (int argc, char **argv)
It is important to note that the emphasis of the previous notation is that argv is a pointer to the starting element of an array whose elements are character pointer types.

4. Changing string Constants
Here's how to do it:
Char *p = "xyz";
P[0] = ' A ';
The compiled device may not cause problems, but when executed it is very likely to encounter a hint similar to an address that cannot be written. The description of this change behavior in K&RC is that the behavior of attempting to change a string constant is undefined.

Although some compilers agree to this behavior. But this kind of writing is not worthy of promotion.

5. Null pointer
In addition to an important exception, converting an integer to a pointer in the C language, the resulting result depends on the detailed C compiler implementation.

The special case of

is constant 0. The compiler guarantees that a pointer converted from 0 is not equal to any valid pointer, for the sake of the documentation of the code, the value of the constant 0 is often replaced by a symbol:
#define NULL 0
It is necessary to remember that when the constant 0 is converted to the pointer use, This pointer must never be dereferenced (dereference is the use of (*P) This class to take the contents of the address), in other words. When we assign 0 to a pointer variable, we absolutely cannot attempt to use what is stored in the memory that the pointer points to. Advantages of "Incorrect boundary" in the

6.C language
    after defining an array int a[10] in the C language, the subscript of the array 0~9 a valid subscript, The subscript 10 already exceeds the range of the array. What are the advantages of this?
The first advantage, see the following example:
for (i = 0; i < i++)
    a[i] = *p++;
Assuming that the user has given the range of begin (0) and end (10), it is necessary to manipulate the unit in between, assuming that the user's given begin and end are the same, and that the above notation completely avoids errors. The number of units to be manipulated at the same time can be end-begin simple, so the premise is that the user's begin and end are to obey the C language "incorrect boundary" usage. For example, if you do not use an improperly called boundary (when the subscript of an array is 1~10 valid), such as code:
for (i = 1; i <=; i++)
    a[i] = *p++;
Enough to finish the initialization or traversal of the array, and so on. The actual operation of the number of units is 10-1+1=10, the calculation process assumes that the program ape in the programming time to forget to add a 1 so very easy to cause the program bug. At the same time, assuming that 1 and 10 are replaced with the begin and end variables, then the user passes the begin and end values of the function as if they were the same value, which would also manipulate the A[begin] value in the array, which would also cause difficulties for the caller to use. The second advantage of the
is that we can use &a[10] as an inference condition. As a buffer or array operation completed a flag, which in practical programming is also quite convenient. Although it is illegal to operate on the value of a[10], it is legal to understand that the operation of &A[10] is specified in ANSI.



7.--n >= 0 and n--> 0
In most C language implementations,--n >= 0 is at least as fast as the equivalent n--> 0, even faster in some C implementations, and the first expression--n >= 0 is calculated first by subtracting 1 from N and then comparing the result to 0. The calculation of the second expression first saves N, then subtracts 1 from N, and finally saves the value to 0.

8. Order of Evaluation
There are only four operators (&&, | |,?:,,) in C that have a defined order of evaluation, operators && and Operators | | The operand of the left side is evaluated first, and the operand on the right side is evaluated only when it is needed. Operator?: There are three operands. In A?b:c, operand A is evaluated first, based on the value of a to find the value of operand B or C (b and C have only one expression to be computed).

and the comma operator. First, the operand of the left side is evaluated, then the value is "discarded", evaluated on the right-hand operand, and the value of the entire expression is the value of the right-hand expression.


Example of a comma operator: a = (1, 2, 3);
A is finally assigned a value of 3.
Note: A comma separating function parameters is not a comma operator. For example, the order of evaluation in F (x, y) is undefined, whereas in the function g ((x and Y) it is the order of the first x after Y, in the latter case, the function g has only one parameter. The value of this parameter is the value of the comma operator in parentheses.
Note: The order in which all other operators in the C language evaluates their operands is undefined. In particular, assignment operators do not guarantee any order of evaluation. Suppose that the occurrence of + + in multiple uses of the same variable in an expression is sometimes unpredictable. Like what:
Y[i] = x[i++];

9. Results of logical operations
The result of a logical operator is a logical value, which is true (1) or False (0). While logic inference is usually agreed upon to treat 0 as false. The non-0 is regarded as true. So the value of the!10 expression is False (0), because 10 non-0 is regarded as true when the non-operation is performed, and true is false.



10. Overflow
There are two kinds of integer arithmetic operations in C language, signed number and unsigned number operation.

There is no overflow in the unsigned number operation, however the sign number operation may overflow when the result of an operation "overflows". It is unsafe to make whatever it is. The method that should be taken when encountering a possible overflow is to cast two operands A and B to an unsigned integer:
if ((unsigned) A + (unsigned) b > Int_max)
complain ();
The Int_max here is a defined constant that represents the maximum possible integer value.

The ANSI C standard defines the Int_max in <limits.h>, assuming that in other C implementations, the reader may need to define the value again and again.

"C Traps and defects" finishing two

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.