This article from Csdn Blog, Source: http://blog.csdn.net/zenny_chen/archive/2008/06/04/2512056.aspx
A:
function declaration: Int find (int* a);
function call: int b = 1;
int n = find (&b);
When this is called, it is actually done: int* a = &b, creates a new integer pointer to B as a local variable within the scope of the Find function.
B:
function declaration: Int find (int& a);
function call: int b = 1;
int n = find (b);
When this is called, the:int& a = B is actually performed, and the variable B is given an alias A, which means that A and b actually represent the same variable, and no new variables are created during the call.
C:
function declaration: int find (int a);
function call: int b = 1;
int n = find (b);
When this is called, it is actually done: int a = B, which is the normal variable assignment operation, a local variable A is created within the scope of the Find function during the call, and its value is the same as B
d:& in the calculation is to take the variable address, such as int* a = &b, is the address of the integer variable B is taken out, assigned to the integer pointer a,a content is the address of B, so a points to B
e:*, when used for calculations, is to take the value in the address pointed to by the pointer, such as int b = *a, which is to assign the contents of the address placed in the integer pointer A to the integer variable b
C language,& Symbols Everyone must be familiar with it, except that it can be used as a bitwise operation "and" and there is a more common function-take the variable address.
Let's look at some simple examples:
#include <stdio.h>
int main (void)
{
int a = 0;
int *p = &a;
printf ("The value is:%d\n", *p);
return 0;
}
In the above code, the pointer p points to the address of variable A. In C + +, each variable has its corresponding address, and the address of the variable is obtained by adding the & symbol before the variable identifier.
So can we write this? int *p = &0x01000;
This is obviously not going to work. Because for a numeric constant, it does not have an address. The reason the variable has an address is because there is a storage unit that identifies the variable (and of course, the variable can be mapped directly 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's going on with the code above?
The address--0x0012ff60 of variable A has been investigated previously, and the pointer p here is actually the address that points to variable a.
First, 0X0012FF60 is used as a int*, and it is equivalent to &a.
Then * (int*) 0x0012ff60 represents the contents of variable A.
Finally, &* (int*) 0x0012ff60 means to remove the dereference of * (int*) 0x0012ff60, which translates into (int*) &a.
Therefore, the & in this case is different from the first one in &. The & here is not the address, because a * (int*) 0X0012FF60 is not a variable, it is not an address. Each variable identifier during compilation, the compiler creates a symbol table for them that holds the various attributes of the variable identifier, such as type, address identifier, and so on. The address ID determines the logical address value after it is connected. In short,& as the fetch address operation, when and only if & followed by a variable or function identifier. So here's the & representation to take off the dereference.
From this we can conclude that:& as a fetch address operation, its behavior results are determined at compile time, and *, dereference operation (or fetch content) operation, its behavior results can only be determined at runtime.
Then look at the following example to deepen the impression:
#include "stdio.h"
int main (void)
{
int a = 0;
int *p = &*&*&a;
printf ("The value is:%d\n", *p);
return 0;
}
,& can also represent references in C + +, which is not much to say.
#include "iostream"
using namespace Std;
int main (void)
{
int a = 0;
int &r = A;
cout << "The value is:" << R << Endl;
return 0;
}
C + + *, & Usage