The biggest misunderstanding in the introduction of C language pointers: int *p=&a; the address of a is assigned to p instead of *p

        On the way to learn C language, you will inevitably encounter many difficulties. I believe that students who learn C and C++ must have heard about pointers, and it is also the basis for everyone to learn data structures in the future.

        Every variable has a memory location, and every memory location defines an address that can be accessed using the & operator, which represents an address in memory. A pointer is sometimes an ordinary variable, but unlike other variables, the pointer contains an address , and in most cases it is the address of other variables .

In the course, the teacher likes to use the following formula to define pointers:

int a = 5;
int *p = &a;

 Many students have doubts at this time: &a is the address of a assigned to p or *p?

 Here comes the important point: at this time &a is assigned to p! ! ! Or we can change the following way of writing to see more clearly .

int a = 5;
int* p = &a;

 It can be seen that this writing method combines int and * together, and we can understand it as:

Declare a variable p, the type of this variable p is int* (note that it is int* not int), and then assign the address of variable a to variable p, that is, "pointer p points to a".

However, unlike other variables, variables of type int * cannot be declared directly consecutively . Let's compare p with q:

int* p,q;

As above, only p is of type int*, and q is still an integer variable of ordinary int type.

int* p,*q;

 As above, p and q at this time are pointers of type int*.

The following writing methods are completely equivalent, depending on personal habits (most people are used to the first one, but I personally think the second one is the easiest to understand):

int *p1;

int* p2;

int * p3;

int*p4;

So let's compare several assignments of pointers:

 The first type (assignment at declaration):

int a = 5;
int* p = &a;

The second (assignment after declaration):

int a = 5;
int* p;
p = &a;

The third (continuous assignment):

int a = 5,*p = &a;

The third assignment needs to be said separately: at this time, &a is still assigned to p instead of *p! ! ! Although it looks very much like  *p = &a at this point , it is actually p = &a . At this time, just declare an ordinary variable with int first, and omit the int of int* in the second sentence, which is exactly the same as the assignment in the first declaration.

At this time, you will find that these three functions are exactly the same except for the different writing methods. 

Error-prone area one:

int a = 5;
int* p;
// 错
*p = &a;

  Cause of error: *p is of type int, and p is of type int*. At this time, the right side of the equal sign is of int* type, and the left side of the equal sign is of int type, and a warning will be reported! (Warning for coercion of different types, and p not only fails to point to a at this time, but also becomes a wild pointer)

Error-prone area two:

int a = 5;
// 错
int p = &a;

 Cause of error: The type of p at this time is int type. At this time, the right side of the equal sign is int* type, and the left side of the equal sign is int type, and a warning will be reported! (Warning for coercion of different types, and p is not a pointer variable at this time, but an ordinary int variable)

 Error-prone area three:

int a = 5;
// 错
int *p = 5;

  Cause of error: The value of p is not initialized. p is just like other ordinary member variables and needs to be initialized. Otherwise, no one knows what value is stored in p at this time, and there must be a problem with the program to fetch content with *. A pointer that operates without knowing where p points to is called a wild pointer.

Summarize

You can use the following two methods to understand the pointer variable assignment problem

The first:

int *p=&a; When the p pointer is declared, * only plays the role of indicating that p is a pointer variable. This way of writing emphasizes that the area pointed to by p is an int type area. So the address of a is assigned to p, not *p.

The second (method mentioned above):

int* p=&a; When the p pointer is declared, its type is int*. This way of writing emphasizes that the type of p is int*. So, assign the address of a to a variable of type int* instead of *p.

Of course, there are many other pointers: char*, int*, long* ... and so on, this example only uses int* for demonstration. The type in front of the asterisk is the type of the area pointed to by this pointer, which can be any data type in C language.

Give a thumbs up if it helps! ! !

Guess you like

Origin blog.csdn.net/m0_46745664/article/details/129665537