Details easily overlooked in C language (article 4)
Preface:The purpose of this article is to record the details that are easy to ignore in the C language. I plan to take some time every day to read books and stick to it. Today is the first article, maybe next month's today is the second article, and next year's today is the first article ?...... I firmly believe that good memory is not as bad as writing. Article 4, fight ~...
Article 1: details easily overlooked in C language (article 1)
Link to article 2: details easily overlooked in C language (article 2)
Article 3: details that are easy to be ignored in C language (article 3)
1,Why can't void * type pointers be involved in arithmetic operations and only assign values, comparisons, and sizeof operations?
The pointer arithmetic operation also contains the number of bytes of the object indicated by the pointer.
2,"&" Cannot be used separately for a non-variable. You cannot use "&" for a literal constant to obtain its address, because the literal constant is stored in the symbol table and there is no address concept. The array name is a pointer constant and the value of the array name cannot be modified. For example, the following statement 1 is equivalent to Statement 2:
Int a [10]; // statement 1int * const a; // statement 2int B [3] [4]; // statement 1int (* const B) [4]; // statement 2int c [3] [4] [5]; // statement 1int (* const c) [4] [5]; // Statement 2
3,Differences between return 0 and exit (0)
(1)Exit ends the running program. If exit is called, it returns the parameter to the OS and the control to the operating system. return exits from the current function and returns the function value, give control to the call function;
(2)Exit is the system call level, which indicates the end of a process; return is the language level, which indicates the return of the call stack.
(3)The exit function is also called implicitly when the main function ends. It deletes the memory space used by the process and returns the error message to the parent process.
(4)The main function returns an integer value equivalent to calling exit for this value.
4,Static modifier is a useful tool to reduce name conflicts. For example, static int a; limits the scope of a to one source file. For other source files, a is invisible. Variable a with the same name can be defined in multiple source files, as long as all variable a is defined as static, or only one of the variables is not static. The static modifier also applies to functions.
5,The C language allows programmers to control the output data volume before writing operations. This control capability is implemented through the library function setbuf. For example: setbuf (stdout, buf); the statement will notify the input/output library, and all output data written to stdout will use buf as the output buffer, the buf buffer content is not actually written into stdout until the buf buffer is filled or the programmer calls fflush directly. The buffer size is determined by the system header file. BUFSIZ definition in. There is an example of C traps and defects:
#include
int main(void){char c;char buf[BUFSIZ];setbuf(stdout, buf);while ((c = getchar()) != EOF)putchar(c);return 0;}
The above program is incorrect. The last time the buf buffer is cleared after the main function is completed, it is part of the cleanup work required for the C Runtime Library before the program is handed back to the operating system. Before that, the buf character array has been released. There are three ways to modify:
(1)Define the buffer array as a static array static char buf [BUFSIZ];
(2)Obtain the buffer array outside the main function;
(3)The buffer is dynamically allocated, and the allocated buffer is not automatically released in the program. Setbuf (stdout, malloc (BUFSIZ ));
Note:
(1)Because the buffer zone is dynamically allocated, the buffer zone will not be released when the main function ends, so that the buffer zone will not be released when the C Runtime Library is cleaned up.
(2)If the malloc call fails, a NULL pointer is returned, and the standard output does not need to be buffered.
(3)For a file opened by a write operation, calling fflush will actually write the content in the output buffer to the file.
6,Why does the length of the first dimension not need to be specified when passing multi-dimensional arrays to a function?
(1)The array elements are stored in the memory according to the "row first" rule. The number of columns is required to determine the number of elements in a row;
(2)The compiler does not need the length of the first dimension of the array when calculating the element address, but requires the length information of all other dimensions;
(3)C/C ++ does not check for out-of-bounds access to arrays, so the compiler does not need to know the length of the first dimension.
7,The C language converts a multi-dimensional array to an array pointer. The following four expressions are equivalent:
a[i][j];*(a[i] + j);(*(a + i))[j];*(*(a + i) + j);
8,In terms of semantics, the subscript operator returns an element reference. For example, Statement 1 and Statement 2 are equivalent:
// Statement 1a [3] = 100; // statement 2int & ri = a [3]; ri = 100;
9,A pointer of the element type cannot be used to receive the return address of a dynamically created multi-dimensional array, because a multi-dimensional array is not semantically equivalent to a pointer pointing to its element type, it is equivalent to a "pointer to an array ". For example, the following usage is correct:
char *q = new char[5];delete []q; char (*p)[4] = new char[5][4];delete []p;
10. (1)A label is a unique identifier with a function scope. No matter which row of the number is defined in the function, how deep the number is nested in the function can be accessed anywhere in the function. A label is generally used in a goto statement. If this label is not used in a goto statement, the label is ignored.
(2)When a local variable has the same name as a global variable, the global variable is masked inside the function. In this case, global variables can be referenced through The unary scope parsing operator in the function, for example,.