sya*_*kim 0 c++ pointers reference
我已经读过它意味着指向指针的指针.但在下面的代码中,我能够更改地址的值.
int main() {
int x = 23; // initializing variable X = 23
int *myVar = &x; // Creating a pointer to the address of X
*myVar = 566; // My attempt at changing the value of X address
cout << x << endl; // Printing out X with new value
}
Run Code Online (Sandbox Code Playgroud)
它解决了.这怎么可能?**是指地址的价值吗?
这里有*
两个不同的东西:
int *myVar = &x;
作为指针的*
手段myVar
*myVar = 566;
这*
意味着您可以访问指针指向的值基本上,
int x = 23; // 'x' is 23
int *myVar = &x; // 'myVar' points to 'x'
*myVar = 566; // Assign 566 to the value that 'myVar' is pointing at (which is 'x')
cout << x << endl; // Print 'x'
Run Code Online (Sandbox Code Playgroud)
你是正确的,**
这意味着指向指针(或访问指针指针的值):
int a = 0; //'a' is 0
int* pa = &a; //'pa' is pointing to 'a'
int** ppa = &pa; //'ppa' is pointing to 'pa'
*pa = 1; //'a' is 1
**ppa = 2' //'a' is 2
Run Code Online (Sandbox Code Playgroud)