It is estimated that many people will use both C programs to use both Pointer and Reference these two things, but what differences and contacts are they in the end, I believe that most people don't know much, today I The difference between the differences and contacts from the perspective of the actual program. The pointer in C / C is actually a variable, and other types of variables are like a variable, which is a four-byte variable (32-bit machine), which is different from other variables that its variable value Is a memory address, pointing to another place in memory. Reference my understanding is an alias, it is a look at Alias on the Linux operating system. Furthermore, a Pointer variable can point to NULL, indicating that it does not point to any variable address, but Reference must be bound to a variable that already exists when declared, and this binding cannot be changed. Here is a program I wrote to verify the differences and contacts of Pointer and Reference, I hope to understand the differences and contacts of Pointer & Reference:
#include "stdafx.h" #include
INT main (int Argc, char * argv []) {int = 1024; int * pi = & ival; // a pointer int & rval = ival; // a reference int jval = 4096; int XVAL = 8192;
COUT << "ival =" << iVal << "/ t"; cout << "& =" << & IVAL << "/ t"; cout << endl; cout << "pi =" << PI < <"/ t"; cout << "& pi =" << & pi << "/ t"; cout << "* pi =" << * pi << "/ t"; cout << Endl; cout << "rval =" << rval << "/ t"; cout << "& rval =" << & rval << "/ t"; cout << Endl; cout << "jval =" << jval << "/ T "; cout <<" & jval = "<< & jval <<" / t "; cout << end1
// change the value of the reference and the pointer pi = & jval; rval = jval;
// print the change << ipl; cout << "ival =" << ival << "/ t"; cout << "& ival =" << "i <<" / t "; cout << endl; cout << "pi =" << pi << "/ t"; cout << "& pi =" << & pi << "/ t"; cout << "* pi =" << * pi << "/ t "; Cout << Endl; cout <<" rval = "<< rval <<" / t "; cout <<" & rval = "<< & rval <<" / t "; cout << Endl; cout <<" jval = "<< jval <<" / t "; cout <<" & jval = "<") << Endl; cout << endl; * pi = xval; cout << "Pi = "<< pi <<" / t "; cout <<" & pi = "<< & pi <<" / t "; cout <<" * pi = "<< * pi <<" / t "; cout < Return 0;} The result of the execution is as follows: ival = 1024 & Ival = 0012FF7CPI = 0012FF7C & Pi = 0012FF78 * Pi = 1024rval = 1024 & rval = 0012FF7CJVAL = 4096 & jval = 0012ff70 IVAL = 4096 & Ival = 0012FF7CPI = 0012FF70 & Pi = 0012FF78 * Pi = 4096rval = 4096 & rval = 0012ff7cjval = 4096 & jval = 0012ff70 PI = 0012FF70 & PI = 0012FF78 * PI = 8192JVAL = 8192 & jval = 0012FF70 If you are interested, you can use other types of variables to test, and print out the size of each variable to occupy memory, I will not put the specific program from the specific program. . If you have any objection to the above view, you welcome email to andyzhange@yahoo.com.cn.