The difference between & and * in C is: & takes the variable address and stores the address in the pointer variable. Dereference a pointer variable to obtain the value it points to.
The difference between & and * in C
Short answer:
& takes the variable address, while * dereferences the address.
Detailed explanation:
Get address (&):
&
Operation operator is used to obtain the memory address of a variable. For example:
int num = 10; int *ptr = #
In the above code, ptr
now points to the memory address of num
.
Dereference address (*):
*
The operator is used to dereference a pointer variable and obtain the value it points to. *
operator to read and write pointer variables. For example:
int num = 10; int *ptr = # *ptr = 20;
In the above code, *ptr
dereferences ptr
and changes The value pointed to, that is, the value of num
becomes 20.
Summary of differences:
Operator | Use |
---|---|
& |
Get variable address |
##*
| Dereference Pointer variable, get the value pointed to
& and
* operators are usually used in pairs. Use
& to get the address, then use
* to dereference the address to access or modify the value.
The above is the detailed content of The difference between & and * in c++. For more information, please follow other related articles on the PHP Chinese website!