Artistic art
As a C programmer, the direct operating memory of the pointer, has the advantages of fast, saving memory in data operation, still a lot of C programmers's favorite. The pointer is a double-edged sword. It uses it. You will find how convenient pointer. On the other hand, you may have a headache, often there will be unexpected problems.
One. What is a pointer:
In fact, the pointer is like other variables, and the difference is that the general variables contain actual real data, and the pointer is an indicator that tells the program to find data in the area where the memory can be found.
This is a very important concept, and many programs and algorithms are designed around the pointer, such as a list.
The pointer is a data type that it also needs to take up four bytes of storage. So the value obtained by SIZEOF (VOID *) is 4.
two. Pointer definition
See the following example:
INT * PNUMBER;
This defines the INT type pointer.
The pointer variable name is first character, which is a habit of programmers to define pointers to increase the readability of the programs, indicating that this is a pointer. In addition, although int * pNumber and int * pNumber are the same, the programming style of the latter is better. such as:
INT * PNUMBER1, * PNumber2; Represents two pointers, * numbers, and variables; contrary, we may define int * pNumber1, pnumber2, which will be a wrong definition, PNumber2 is not a pointer.
three. The advantages of pointer a. The means for modifying calls to the function;
b. Support C dynamic distribution subroutine
c. Efficiency of certain subroutines can be improved
d. Support for dynamic data structures (such as binary tree, linked list)
Fourth, pointer assignment and conversion:
a. The same type is directly assigned, and the heterogeneous type is converted.
b. Forced conversion: can be hardally converted to the specified type
C.CHAR * P; (int *) P Forced P to convert the P to INT type, remember to pay attention to the size of two types during the conversion process, and there may be data loss in the large turn (such as int to double)
d. Take the void *:
C in the Void * type can assign a value to any type of pointer, and vice versa
Forced conversion is required in C
Void * can be seen as infinity can accept any type assignment, vice versa INT * P = 9; void * t = p (correct); p = T (error)
e. It is necessary to force the void *.
Fives. Pointers and arrays
Without the subscript of the number of group names to return to the start address of the array, that is, the address of the first element, there are two ways to access the array: array subscript and pointer arithmetic. E.g:
Char * pchar;
CHAR CHS [100];
PCHAR = CHS; so PCHAR will point to the first address of the CHS array.
six. Array and reference
a. Quote is just the alias of the variable, not the pointer to the variable (distinguishes that the address operator "&") does not account for memory space, and the corresponding variables referenced to the variable reference will also change. b. You can't make a reputation operation c.
Seven. Dynamic allocation and recycling of pointer space
Dynamic allocation is a key technique for pointer. It is used to allocate memory and make pointers to point to them without having to define variables. Allocated memory, don't forget to recycle. You dynamically assign a memory space, but it will never be automatically deleted. That is, this memory space will always exist until you tell your computer that you have already used. As a result, you didn't tell the computer that you no longer need this memory space, so it will continue to occupy a waste of memory, and even your program is running, other programs are running. It still exists. When such problems accumulate to a certain extent, it will eventually cause the system to crash. So this is very important, after you run it, please release its space. Eight. Easy to appear when using pointers
The author has the following reasons for pointer errors (personal experience, welcome everyone):
1. The pointer is not initialized.
The initialization of the pointer is not a definition of the pointer, but the value of the pointer variable storage is an invalid value. For example, define float a; this A will assign an address, but the initial value is a messy data. Similarly, float * a; also assigns an address for A, the initial value is also a messy data. Initialization can increase the a = null, which can increase if (a == null) in the later program to determine if the pointer is valid, otherwise it will not. Or assign or specify a space for a pointer. Such as Float * a = new float; or float b; float * a = & b; can point to a plurality of memory for the pointer to implement initialization.
2. Pointer
The pointer offshore is a more difficult to capture errors. If the test is not comprehensive, it is not easy to find. For the space size assigned to the pointer, the programmer must pay attention to it.
3. Pointer to partial variable
The pointer is a variable that records a block memory start address. To make the pointer, you must ensure that this memory is valid. With the memory space allocated by NEW, it is always valid as long as it is not delete. But for a pointer to a variable address, the programmer must know the scope of the variable. If the scope of the variable is left, the memory space of the variable will be automatically reclaimed by the system, and an error will occur when using a pointer. This is the most prone to errors in the program.
4. Transfer pointing to pointer
Some programmers who initially target C often write such procedures:
Char * pchar = new char;
CHAR CHS;
PCHAR = & chs;
Delete pchar;
Their purpose is to pass the CHS content to the memory pointed to the PCHAR pointer. But write this, will make the spatial spatial spatial spatial point to Pchar, because the address is unable to get it. Conditioned wild pointer. Will cause memory leakage. Moreover, an exception error occurs when the Delete PCHAR is called. Because it is not a new space that cannot be deleted using Delete. Because Pchar has turned to the address of the variable pointing to CHS.
The art of pointers is far more than this, and it is insecting to discuss our enthusiasts. The pointer has brought too much convenience to our procedure. Similarly, it also has a lot of difficult problems. How to better control the pointer, can be said to be a means of C programmers to improve your own.
"Note" This article refers to the C introduction note of the blog network, and integrates the author's own understanding of the pointer.