table of Contents
Last talk about pointer links:
" * "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 int
type. Then I want to ask at this time, &a
what 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 a
variable is a int
type, 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 p
as 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 int
type. Then the variable on the left p
is 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 *p
overall data type on the left side of the equation is the int
type.
The second example:
int *p;
int **q;
q = &p;
On the left side of the equation, q
the data type of the int**
variable p
is 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 &p
data 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.
The explanation is as follows:
- 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.
- It mainly prints the byte size of three pointer variables, among which the sizeof function is used.
- 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.
The 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.
The explanation is as follows:
- As can be seen from the figure, we have defined an integer variable, the format is hexadecimal.
- Then three pointer variables p1, p2, and p3 are defined respectively. And take the address of its num integer variable separately.
- The figure shows a forced conversion operation. As for why, take one of the examples
char* p1 = (char*)#
p1
The data type on the left side of the equation is char*
type, then num
the data type of the variable is int
type, 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 &num
overall 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.
- When printing, we use the
%x
format output, this symbol is mainly output hexadecimalint
type. - 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 *p
Take one byte,
short *p
take two bytes and
int *p
take 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, itchar *p
can be equivalent tosizeof(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! ! !