The third part of the pointer (" * "and" & ", the step length of the pointer) ---- 2021.1.31

Last talk about pointer links:

The second part of the pointer (definition, initialization and use of pointer variables) ---- 2021.1.29

" * "versus" & "

The two symbols "*" and "&" are two symbols that we frequently use when we use pointers.
First, take the contents of the memory pointed to by the pointer variable through the "*" symbol, and then we operate on that memory area.
The "&" symbol is to take the address of the memory area to be operated on, that is, to obtain the address number and assign it to the pointer variable we defined.

Everyone should be aware of the two points mentioned above. But I want to use this concept to talk about the data type matching of expressions.

For example, there is an expression int *p = &a;
we all know, in fact, the above expression can be expressed in another form, but also to give you a better understanding, as shown below.

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

In the last lecture, we already know that p is a variable, so the data type of this variable is int *type. The variable on the right side of the expression is a, and the data type of this variable is inttype. Then I want to ask at this time, &awhat data type is this whole?

Next, I will introduce a concept to everyone. When using it , if you take an expression *, you will lower the expression one level *. If you take the expression &, you will add one level to the expression* .

How do you understand this sentence?
In the above example, because the avariable is a inttype, then according to the above concept, to take the expression &, you need to add one level to the expression *, so the final data type of &a is int*type, which is exactly the same pas the data of the variable on the left side of the expression . The type is the same, and finally the data type matching problem on both sides of the expression is realized.

Then give two more examples.

The first example: the *p = 100;
right side of the equation, as the name suggests, is of inttype. Then the variable on the left pis a int*type. From the above concept, it can be seen that taking an expression *will lower the expression by one level *, so the final *poverall data type on the left side of the equation is the inttype.

The second example:

int *p;
int **q;
q = &p;

On the left side of the equation, qthe data type of the int**variable pis int*type, and the data type of the variable on the right side is type. Then, from the concept, if you take &the expression, one level will be added to the expression *, so the final &pdata type is int**type, and both sides of the expression match .

Pointer step

Before talking about the pointer step size, I want everyone to understand the size of the pointer variable.
For example, as shown in the figure below.

Insert picture description hereThe explanation is as follows:

  1. We have defined three pointer variables p1, p2, and p3. And the data types of these three pointer variables are different, I also marked them with a red box in the figure.
  2. It mainly prints the byte size of three pointer variables, among which the sizeof function is used.
  3. The final result found that the byte size of the three variables are the same. That's because, as mentioned in the previous lecture, pointer variables are mainly used to store address numbers, and our compiler at this time is 32-bit, so we have reached a conclusion that no matter what type of pointer, the size is only the same as The compiler type of the system is related . If you are using a 64-bit compiler, the result will naturally become 8.

So what if we define a double pointer, will the result be the same? As shown below.

Insert picture description hereThe result is obvious and the same, because a double pointer is also a pointer variable, as long as it is a pointer variable, it will only store the address number, which is the address. So it confirms the conclusion in the above explanation.

Then we think about a problem, since no matter what the type size in bytes of a pointer variable can hold all the same, then why there will be so many data types it, such as int*, char*, short*, double*and so on.

For example, as shown in the figure below.

Insert picture description hereThe explanation is as follows:

  1. As can be seen from the figure, we have defined an integer variable, the format is hexadecimal.
  2. Then three pointer variables p1, p2, and p3 are defined respectively. And take the address of its num integer variable separately.
  3. The figure shows a forced conversion operation. As for why, take one of the examples
char* p1 = (char*)#

p1The data type on the left side of the equation is char*type, then numthe data type of the variable is inttype, and as we know from before, in order to match the data type of the expression, when performing &operations, the expression needs to be upgraded by one level *, so the &numoverall data type is int*type . So we all know, int*and char*type does not match, it will be cast on the right side of the equation. Will be int*replaced with char*type.

  1. When printing, we use the %xformat output, this symbol is mainly output hexadecimal inttype.
  2. As you can see in the final result, the output of the three variables is different. Why is this? Next, the concept of step size is formally introduced. First, the char type output result is 04, the short type output result is 0304, and the int type output result is 01020304. You may find that this is related to the type of the pointer variable we defined.
    char *pTake one byte,
    short *ptake two bytes and
    int *ptake 4 bytes.
    So in fact, we can understand this step length as the width, that is, by *taking the content of the memory space pointed to by the pointer variable, the width of the memory is obtained (step Long) is related to the type of the pointer variable itself.
    Then the operation of obtaining the step length of the pointer variable can be realized by the sizeof function. For example, it char *pcan be equivalent to sizeof(char).
    Step size : sizeof( *Erase the nearest pointer variable and pointer variable , and the remaining types)
    Give another example
    int **p;//即sizeof(int *) = 4;

As for the concept of step size, we will mention it later on array pointers or pointer arrays. It is okay if you don't understand it, and we will explain it in detail at that time.

Concluding remarks

If you think this article is good, remember to like it and support it! ! !

Guess you like

Origin blog.csdn.net/qq_40544107/article/details/113461382
Recommended