1. Value Delivery
void f (int p) {
printf ("\n%x", &p);
printf ("\n%x", p);
P=0xff;
}
void Main ()
{
int a=0x10;
printf ("\n%x", &a);
printf ("\n%x\n", a);
f (a);
printf ("\n%x\n", a);
}
As we can see in the example above, the int a=0x10, where the address is 0x12ff44, the value is 10, and when F (a) is invoked, the value passed to P is 10, but the address of P is 0x12fef4, and when P=0xff is changed, it changes the contents of the address 0x12fef4. Does not change the content in the 0x12ff44, so call F (a), and the value of the after a is still 0x10, so the value pass cannot change the value of the variable. The schematic diagram is as follows:
2. Reference Delivery
void f (int & P) {
printf ("\n%x", &p);
printf ("\n%x", p);
P=0xff;
}
void Main ()
{
int a=0x10;
printf ("\n%x", &a);
printf ("\n%x\n", a);
f (a);
printf ("\n%x\n", a);
}
Through the above reference passing case we can see that when we call F (a), the address of a is passed to P, so the address of P and a is 0x12ff44, so p is a, change p can change a. The schematic diagram is as follows:
3. Pointer delivery
void f (int*p) {
printf ("\n%x", &p);
printf ("\n%x", p);
printf ("\n%x\n", *p);
*p=0xff;
}
void Main ()
{
int a=0x10;
printf ("\n%x", &a);
printf ("\n%x\n", a);
f (&a);
printf ("\n%x\n", a);
}
The case passed by the pointer we can see that the call F (&a) is the address of a 0x12ff44 passed to P, then *p point to the content of a, change *p, the contents of a naturally changed, the schematic diagram is as follows:
4. Reference passing of pointers
void f (int*&p) {
printf ("\n%x", &p);
printf ("\n%x", p);
printf ("\n%x\n", *p);
*p=0xff;
}
void Main ()
{
int a=0x10;
printf ("\n%x", &a);
printf ("\n%x\n", a);
int *b=&a;
printf ("\n%x", &b);
printf ("\n%x", b);
printf ("\n%x\n", *b);
f (b);
printf ("\n%x\n", a);
}
in order to use the reference pass of the pointer we want to create a new pointer B, and then pass the reference B to P, in fact P is a copy of B, *p=*b all point to a, so changing the contents of the *p changes the content of a. The schematic diagram is as follows:
Let's see what happens if you don't pass a reference to the pointer.
void f (int*p) {
printf ("\n%x", &p);
printf ("\n%x", p);
printf ("\n%x\n", *p);
*p=0xff;
}
void Main ()
{
int a=0x10;
printf ("\n%x", &a);
printf ("\n%x\n", a);
int *b=&a;
printf ("\n%x", &b);
printf ("\n%x", b);
printf ("\n%x\n", *b);
f (b);
printf ("\n%x\n", a);
printf ("\n%x\n", b);
}
From the results we can see that when we call F (b), the contents of B are passed to p, but &b is not the same as &p, although both *p and *b point to a. The schematic diagram is as follows:
5. Error cases
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void Allocate (char* p,int Size) {
printf ("\n%x", &p);
printf ("\n%x", p);
p= (char*) malloc (size);
}
void free (char* p) {free
(p);
}
void Main ()
{
char *str=null;
printf ("\n%x", &str);
printf ("\n%x", str);
Allocate (str,100);
strcpy (str, "Hello world!");
printf ("\n%s", str);
Free (str);
printf ("\nstr=%s", str);
}
When executing strcpy (str, "Hello world!"), the unhandled exception in cpoint.exe:0xc0000005:access violation is reported, because we are using pointer passing, From the run result we can see that Str's address is 0x12ff44, and when allocate (STR,100) is invoked, the P is passed to STR, which is 0, so p is 0, but &p is not the same as &STR, so running p= (char* malloc (size), the 0x12fef0 is allocated 100 bytes, and the 0x12ff44 is not allocated bytes, so *str is still empty. So it will be an error.
5. The right case
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void Allocate (char*& p , int size) {
printf ("\n%x", &p);
printf ("\n%x", p);
p= (char*) malloc (size);
}
void free (char* p) {free
(p);
}
void Main ()
{
char *str=null;
printf ("\n%x", &str);
printf ("\n%x", str);
Allocate (str,100);
strcpy (str, "Hello world!");
printf ("\n%s", str);
Free (str);
}
Because the pointer reference passes a copy of the pointer, so &STR and &p are the same address, so assigning content space to P is a space for STR, so there's no problem.