① What is defined by int * p is p instead of *p, and the type of p is a pointer to int type (int *).
②The * here is a value operator, which operates/retrieves the value of the address pointed to by p. This is completely different from the * used in the definition in ①! Don't get confused!
③& in C is the address symbol. This sentence assigns the address of i to p (instead of *p, it is easy to understand if the definition and initialization operations are separated)
There is no statement about what type *p is. If I have to say it, here p is a pointer to int type data, so *p can be regarded as int type.
When I first learned C++ pointers, I had exactly the same confusion as the questioner. At that time, I always felt that getting the content of p (that is, the operator) is equivalent to getting the address of i. Then p itself is not a pointer to the address. In other words, assuming that and & are inverse operations like + and -, if * is moved to the right, wouldn't there be two &? It’s even stranger when you analyze the quotes. . .
Actually, as the bold part in zodiacg's answer says The * and & used when defining pointers and reference variables are purely markers, and do not have any meaning of obtaining content or address. They are not related to operations. The symbol has nothing to do with it. In other words
int i = 2;
int* p = &i;
*i = 0;
int& r = i;
The * in the second line and the third line have different properties. The former is just a type modifier, while the latter is an operator like the + operator. There is no relationship between the two, it is just stipulated in the standard. The same applies to the & in the fourth and second lines.
I feel that the subject of the question is very rigorous in thinking. This is how you learn C++ so that you can deeply understand the meaning of each statement and not be "trapped" by it, but use it better and flexibly. May you fall in love with C++.
Your concept is ridiculously wrong, but I don’t want to talk about the type of p is int*, because even a second-grade elementary school student knows it. I want to talk about how to understand the type definition of C style.
Taking the example in the question, understanding this type is like solving an equation int *p It means that the type of *p is int. Since p points to an int, then p is a variable that stores the address of an int type variable. This is too simple, so the last point is slightly more complicated: char*(*p[10])(int**), and start "solving equations" (a : t means the type of a is t):
The result has been derived. p is an array of 0 -> 9, which stores a pointer, and the pointer points to a function. The type of the function is char*(int**), which is int** => char*.
The latter sentence is equivalent to
Add a code for convenience:
① What is defined by
int * p
isp
instead of*p
, and the type ofp
is a pointer to int type (int *
).②The
*
here is a value operator, which operates/retrieves the value of the address pointed to byp
. This is completely different from the*
used in the definition in ①! Don't get confused!③
&
in C is the address symbol. This sentence assigns the address ofi
top
(instead of*p
, it is easy to understand if the definition and initialization operations are separated)There is no statement about what type
*p
is. If I have to say it, herep
is a pointer to int type data, so*p
can be regarded as int type.The type of p is pointer
*p is in a sense int &p indirect reference
It is necessary to distinguish between the three
&
situations.Here
i
is of typeint
,p
is of typeint*
, and*p
is of typeint
.The type of
a
here is alsoint
, notint&
.Here the
b
type isint&
, and the function return value type is alsoint&
.When I first learned C++ pointers, I had exactly the same confusion as the questioner. At that time, I always felt that getting the content of p (that is, the operator) is equivalent to getting the address of i. Then p itself is not a pointer to the address. In other words, assuming that and & are inverse operations like + and -, if * is moved to the right, wouldn't there be two &? It’s even stranger when you analyze the quotes. . .
Actually, as the bold part in zodiacg's answer says The * and & used when defining pointers and reference variables are purely markers, and do not have any meaning of obtaining content or address. They are not related to operations. The symbol has nothing to do with it.
In other words
The * in the second line and the third line have different properties. The former is just a type modifier, while the latter is an operator like the + operator. There is no relationship between the two, it is just stipulated in the standard. The same applies to the & in the fourth and second lines.
I feel that the subject of the question is very rigorous in thinking. This is how you learn C++ so that you can deeply understand the meaning of each statement and not be "trapped" by it, but use it better and flexibly. May you fall in love with C++.
&
This symbol has different meanings when "modifying" [type] and [variable].&a
is the address of aint &b = a
defines a variable, which is a reference (note that this is notint &b = &a
)Your concept is ridiculously wrong, but I don’t want to talk about the type of p is int*, because even a second-grade elementary school student knows it. I want to talk about how to understand the type definition of C style.
Taking the example in the question, understanding this type is like solving an equation
int *p
It means that the type of *p is int. Since p points to an int, then p is a variable that stores the address of an int type variable. This is too simple, so the last point is slightly more complicated:char*(*p[10])(int**)
, and start "solving equations" (a : t means the type of a is t):The result has been derived. p is an array of 0 -> 9, which stores a pointer, and the pointer points to a function. The type of the function is char*(int**), which is int** => char*.