C++指针&和*的使用
pointer is a variable that stores the address of another variable. start from scratch, we already know what is variable. A variable store in memory have not only information of its value, but also its memory address.
int x = 5; // declare and initializa a variablethen we can use “address-of operator” - & to get the address of x!
int* ptr = &x //get the x's address and pass it to ptryou may ask
- why
* ptris ainttype data- you are saying that
ptris a pointer to anint.
- you are saying that
- where should I place the asteroid
*? - Is that with end ofint, or with beginning ofptr- only matter of style
- However, the first style (
int* ptr) is often preferred because it makes it clear thatptris a pointer to anint. This can be especially helpful when declaring multiple pointers in a single statement: int* ptr1, ptr2; // Only ptr1 is a pointer, ptr2 is an int- better declare one pointer per line
- since the name of
&is “address-of operator”取地址符, what is the name of*as operator- it is called as “dereference operator”解引用符
- BEAWARE that
&also have another way to use, which is referenceint &reference_value = valuemeans give it a new name, and all operations onreference_valueis same as directly onvalueYou can use the pointer to access the value ofx:
int value = *ptr; // Dereferencing the pointer to get the value of xExample
example code
#include <iostream>
int main() {
int x = 10; // Declare an integer variable x and initialize it to 10
int* ptr = &x; // Declare a pointer to an integer and initialize it with the address of x
std::cout << "Value of x: " << x << std::endl; // Output the value of x
std::cout << "Address of x: " << &x << std::endl; // Output the address of x
std::cout << "Value of ptr: " << ptr << std::endl; // Output the value of ptr (address of x)
std::cout << "Value pointed to by ptr: " << *ptr << std::endl; // Output the value pointed to by ptr (value of x)
return 0;
}result
Value of x: 10
Address of x: 0x7ffee4b3c8ac
Value of ptr: 0x7ffee4b3c8ac //ptr = &x
Value pointed to by ptr: 10 //*ptr = xp.s. about address on a 64-bit system, a memory address might look like 0x7ffee4b3c8ac. Here’s why:
- 0x: This prefix indicates that the number is in hexadecimal format.
- 7ffee4b3c8ac: This is the actual address in hexadecimal. Each digit represents 4 bits, so a 64-bit address will have up to 16 hexadecimal digits.
summary
xis a variable that stores a value.&xis the address of the variablex.ptris a pointer that stores the address ofx.*ptris the value stored at the addressptrpoints to.