Using * to declare pointers is mostly a matter of convention, but there is a reason of consistency: the * in the declaration int *p means int is the type of *p.
It might seem more consistent to write int &p = &n as p is initialized to the address of n but this convention would not hold for double pointers: int **pp defines pp as a pointer to a pointer to an int, yet pp cannot be initialized with &(&n).
int a=5;
int b=a; //b= 5
int c=&a; //c= address of a variable
int *d=&a; //d is a
If we change **b** to another number **a** will be not changed.
But if change **d** to another number **a** will be changed.
Using
*
to declare pointers is mostly a matter of convention, but there is a reason of consistency: the*
in the declarationint *p
meansint
is the type of*p
.It might seem more consistent to write
int &p = &n
asp
is initialized to the address ofn
but this convention would not hold for double pointers:int **pp
definespp
as a pointer to a pointer to anint
, yetpp
cannot be initialized with&(&n)
.In C
*
in a variable definition means "pointer to". In an expression the&
operator means "address of", while*
operator means "dereference".所以实际上:
Why not use
&
as inint& p
? Mostly because the syntax isn't set up that way.It's worth noting that in C++ the
&
comes back to mean "reference":请注意,无需进行任何特殊处理,只需弄清楚即可。
在像c或c ++这样的编程语言中, 我们有指针*和地址&。
何时以及如何使用:
实际上,我们将指针用于对象,列表等。 使用指针,您不会消耗大量内存。 库使用指针对数据进行操作,而无需在内存中创建新位置。