The difference between reference and pointer in C and C + +

Source: Internet
Author: User

1: A conceptual difference between a reference and a pointer

A reference is an alias for a variable, such as

int m;

int &n=m;

Reference as an alias. It is logically not independent, its existence is dependent, so the reference must be initialized at the outset, and the object it refers to cannot be changed throughout its life cycle (it can only be attached to the same variable from beginning to end). Any action on the reference is an action on the variable. References are typically parameters and return values that are used to handle functions.

Usage Rules:

A: References must be initialized at the time of creation (pointers can be assigned at any time)

B: The reference must be associated with a certain legitimate memory unit. There is no null reference.

C: Once the reference is initialized, you cannot change the variable that the reference points to.

#include <iostream> #include <cstdio>using namespace Std;int main () {  int a=1;  int b=2;  int &k=b;  printf ("%d%d%d   %p    %p    %p\n", a,b,k,&a,&b,&k);  K=a;  printf ("%d%d%d   %p    %p    %p\n", a,b,k,&a,&b,&k);  return 0;}

Obviously, the reference alias K and B have the same address, which indirectly changes the B variable. (complies with A,B,C rules)

A pointer is a variable that holds the address of another variable,

int m;

int *n=&m;

So the pointer can manipulate the address of the variable directly, so the programmer has a more flexible choice, but the pointer will bring more unsafe factors.

Thus, as a pointer to a variable, it is logically independent and can be changed, including the change in the address it points to and the data stored in the address it points to.

3: Reference to the difference between a pointer when a function is passed:

In C + +, pointers and references are often used for parameter passing of functions, however, pointer passing parameters and reference passing parameters are inherently different:

A pointer pass parameter is essentially the way a value is passed, which is passed an address value. In the process of value passing, the formal parameters of the called function are treated as local variables of the modulated function, that is, the memory space is opened in the stack to hold the values of the arguments put in by the key function, thus becoming a copy of the argument. The characteristic of value passing is that any operation of the function on the formal parameter is done as a local variable, without affecting the value of the argument variable of the main key function. (Here is the address value of the argument pointer itself will not change)

In the process of reference passing, the formal parameters of the called function also open up the memory space in the stack as a local variable, but the address of the argument variable that is put in by the key function is stored. Any operation of the modulated function on the formal parameter is handled as an indirect addressing, that is, the argument variables in the central Melody function are accessed through the address stored in the stack. Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

The more subtle differences between the two are:

With respect to the usage of the const described above, there is no int& const A, and the const int& A is a, the former guideline with itself is the alias can not be changed, it is of course, so do not need this form, the latter refers to the value of the reference can not be changed.

The "sizeof reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself;

Pointer and reference self-increment (+ +) operation has different meanings;

References are type-safe, while pointers are not (references are more type-checked than pointers

The use of references in function parameter passing. (similar to fixed static type, but can be changed freely)

#include <iostream>using namespace std;void Func (int &n) {  n++;  cout<< "Now the value is" <<N<<ENDL;} int main () {  int a=10;  Func (a);  Func (a);
    • Related articles recommended:
    • 01-OBJECTIVE-C Overview of Learning Notes
    • The easy-to-ignore details of C language (fourth article)
    • Contents of file operation in C language
    • This article from: Hobby Linux Technology Network
    • This article link: http://www.ahlinux.com/c/9014.html

The difference between reference and pointer in C and C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.