Summary of C language pointer learning experience

  • 2020-04-01 21:32:38
  • OfStack

    This summary of learning experience of C language pointer is mainly a little record in the process of learning C pointer since I joined the company. Instead of repeating the conceptual things that the book makes clear, the document explains things that are unclear or hard to understand, in the hope of achieving three goals

1. By writing these things, I can clarify my vague knowledge of C in my head.
2. Give hints and help to colleagues who have just transferred to C.
3. I also hope you can check whether there is any misunderstanding in the document.
1. Conceptual decomposition of pointer
          A pointer is a special variable whose stored value is interpreted as an address in memory. To figure out a pointer, you need to figure out the contents of the square of the pointer:

1. Type of pointer
2. The type the pointer points to
3. The value of the pointer, or the memory area that the pointer points to
4. There is also the memory area occupied by the pointer itself
Declare a few Pointers for example:

Example 1:

(1) int * PTR;

(2) the char * PTR.

(3) int * * PTR.

(4) int (* PTR) [3].

(5) int * (* PTR) [4];

1.1 type of pointer
          Syntactically, you just remove the pointer name from the pointer declaration statement and the rest is the type of the pointer. This is the type that the pointer itself has. Therefore, analyze the types of each pointer in the following example:

(1) int * PTR; // pointer is of type int *

(2) the char * PTR. // pointer is of type char *

(3) int * * PTR. // pointer is of type int **

(4) int (* PTR) [3]. // pointer of type int(*)[3]

(5) int * (* PTR) [4]; // pointer of type int *(*)[4]

So isn't it easy to figure out the type of pointer?

1.2 the type to which the pointer points
        When you use a pointer to access the area of memory that the pointer points to, the type of pointer points to determines what the compiler will think of as the contents of that area of memory. Syntactically, you simply remove the pointer name and the pointer declaration * to the left of the pointer declaration statement, and all that is left is the type the pointer points to. Such as:

(1) int * PTR; // pointer points to an int

(2) the char * PTR. // pointer to the type is char

(3) int * * PTR. The type of the // pointer is int *

(4) int (* PTR) [3]. // pointer to an int()[3]

(5) int * (* PTR) [4]; // pointer to an int *()[4]

In the arithmetic operation of a pointer, the type to which the pointer points plays a big role.

      The type of pointer (that is, the type of pointer itself) and the type to which the pointer refers are two concepts. As you become more familiar with C, one of the keys to mastering Pointers is to divide the notion of a type mixed with Pointers into the notion of a pointer's type and the notion of the pointer's type. Some textbooks are poorly written, which confuse the two concepts of Pointers, so they are inconsistent and confusing.


Related articles: