In the C language, everyone must be familiar with the & symbol.
In addition to bitwise operations, it also provides more common functions-getting variable addresses.
Let's take a look at the following simple example:
- # Include <stdio. h>
- Int main (void)
- {
- Int A = 0;
- Int * P = &;
- Printf ("the value is: % d/N", * P );
- Return 0;
- }
In the above Code, the pointer P points to the address of variable. In C/C ++, each variable has its corresponding address. You can get the address of the variable by adding the & symbol before the variable identifier.
So can we write this? Int * P = & 0x01000;
This is obviously not acceptable. For a numeric constant, it has no address. The reason why a variable has an address is that it requires a storage unit to identify the variable (of course, the variable can also be directly mapped to a register ).
Let's look at the following code:
- # Include "stdio. H"
- Int main (void)
- {
- Int A = 0; // & A = 0x0012ff60
- Int * P = & * (int *) 0x0012ff60;
- Printf ("the value is: % d/N", * P );
- Return 0;
- }
What is the above Code?
We have previously investigated the address of variable A -- 0x0012ff60, so the pointer P here actually points to the address of variable.
First, use 0x0012ff60 as the int *. In this case, it is equivalent to &.
Then * (int *) 0x0012ff60 indicates getting the content of variable.
Finally, "& * (int *) 0x0012ff60" indicates that the X (int *) 0x0012ff60 is removed, which is equivalent to (int *) &.
Therefore, the & here is different from the & In the first example. Here & is not the address, because a * (int *) 0x0012ff60 is not a variable and has no address. During compilation, the compiler creates a symbol table for each variable identifier, which stores various attributes related to the variable identifier, such as the type and address identifier. The logical address value can be determined after the connection. In short, & acts as an address fetch operation. If & is followed by a variable or function identifier. So here '&' indicates removing the Unquote.
As a result, we can conclude that the behavior result is determined at the time of Compilation When & is used as the address fetch operation, and *, The unreference operation (or content fetch) operation, the behavior result can only be determined at runtime.
Let's take a look at the following example to deepen our impression:
- # Include "stdio. H"
- Int main (void)
- {
- Int A = 0;
- Int * P = & * &;
- Printf ("the value is: % d/N", * P );
- Return 0;
- }
However, the & Symbol cannot be used multiple times, but can only be placed before the variable identifier or one unreference. The following is an example.
int main(void){ int a = 100; int *p = &a; int **pp = &p; int **qq = &*&*pp; // OK int **rr = *&*&pp; // OK int **ss = &&**pp; // ERROR}
The current expression is not a left value after a & to remove the reference. And & must be placed before a left value, because only the left value can ensure that the reference is valid.
In C ++, & can also represent references. This is not much to say.
- # Include "iostream"
- Using namespace STD;
- Int main (void)
- {
- Int A = 0;
- Int & R =;
- Cout <"the value is:" <r <Endl;
- Return 0;
- }