比如
int i=2, *p=&i;
在c++中,*p
这个表达式的类型是int的引用即int &
,而c中并没有引用这个概念,所以在c中*p
的类型是什么?难道是int
?如果是这样的话太搞了吧
怪我咯2017-04-17 12:03:00
int i=2;
int * p=&i; // ①int* p和int *p都一样
The latter sentence is equivalent to
int * p;
p = &i; //③
Add a code for convenience:
*p = 23; //②此时i也等于23
① 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.
天蓬老师2017-04-17 12:03:00
It is necessary to distinguish between the three &
situations.
int i = 2,
*p = &i;
Here i
is of type int
, p
is of type int*
, and *p
is of type int
.
int &a = i;
The type of a
here is also int
, not int&
.
int& someFunc(int &b)
Here the b
type is int&
, and the function return value type is also int&
.
大家讲道理2017-04-17 12:03:00
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++.
PHP中文网2017-04-17 12:03:00
&
This symbol has different meanings when "modifying" [type] and [variable].
&a
is the address of a
int &b = a
defines a variable, which is a reference (note that this is not int &b = &a
)
高洛峰2017-04-17 12:03:00
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):
char*(*p[10])(int**)
=> p : char*(*[10])(int**)
=> p[0..9] : char*(*)(int**)
=> *(p[0..9]) : char*(int**)
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*.