This C language pointer learning experience is primarily a bit of history in the C pointer learning process since I started my career. In this document, I will not repeat the conceptual things that are clearly stated in the book. I will explain some things that are unclear or obscure, hoping to achieve the following three purposes:
1. Write these things to clarify the vague knowledge about C in my head.
2. Give some tips and help to colleagues who are new to C.
3. You may also want to check whether there is any misunderstanding in the document.
1. Conceptual decomposition of pointers
A pointer is a special variable. The value stored in it is interpreted as an address in the memory. To understand a pointer, we need to understand the four aspects of the pointer:
1. pointer type
2. Type pointed to by pointer
3. the pointer value or memory zone pointed to by the pointer
4. There is also the memory zone occupied by the pointer itself
First, declare several pointers for example:
Example 1:
(1) int * ptr;
(2) char * ptr;
(3) int ** ptr;
(4) int (* ptr) [3];
(5) int * (* ptr) [4];
1.1 pointer type
From the syntax perspective, you only need to remove the pointer name in the pointer declaration statement, and the rest is the pointer type. This is the type of the pointer. Therefore, we analyze the types of pointers in the following example:
(1) int * ptr; // the pointer type is int *
(2) char * ptr; // the pointer type is char *
(3) int ** ptr; // the pointer type is int **
(4) int (* ptr) [3]; // the pointer type is int (*) [3]
(5) int * (* ptr) [4]; // the pointer type is int * (*) [4]
So is it easy to find the pointer type?
1.2 type pointed to by pointer
When you access the memory area pointed to by the pointer, the type pointed to by the pointer determines what the compiler will regard the content in the memory area. In terms of syntax, you only need to remove the pointer name and the pointer declarative * on the left of the name in the pointer declaration statement, and the rest is the type pointed to by the pointer. For example:
(1) int * ptr; // The Pointer Points to an int type.
(2) char * ptr; // The Pointer Points to a char type.
(3) int ** ptr; // The type pointed to by the pointer is int *
(4) int (* ptr) [3]; // The type pointed to by the pointer is int () [3]
(5) int * (* ptr) [4]; // The type pointed to by the pointer is int * () [4]
In pointer arithmetic operations, the type pointed to by the pointer has a great effect.
The pointer type (the pointer type) and the pointer type are two concepts. When you are more and more familiar with C, you will find that the concept of "type" mixed with pointers is divided into two concepts: "pointer type" and "pointer type, it is one of the key points of mastering pointers. Some textbooks are poorly written, and the two concepts of pointers are stirred up together. Therefore, the book is difficult to read.