A brief analysis of C + + pointers and references

Source: Internet
Author: User

Summary
This article describes the concept of pointers and references in C + +

What is a pointer
Type of pointer
The type that the pointer points to
Pointer expression
Pointers and functions
What is a reference
Pointer reference differences
The same points and different points of pointers and references

* * What is a pointer * *
The pointer is a variable that holds the address, and when the pointer points to a variable, the pointer holds the address of the variable, and the pointer can be used to directly take the value of the variable, as long as the pointer is in front of the
(That is, the value of the variable being pointed to)

! [Pointer Signal] (http://img.blog.csdn.net/20150605232124686)
* * Type of pointer * *


int  *p ; // pointer type int  *char * P ; // char *int  **  p; // int  **  int  (*p ) [3 ]; // int  *3  int  * ( *p ) [4 ]; // int  **  4  


* * The type pointed to by the pointer * *


int   *p ; // int  char *p ; // char int  **  p;     // int  * int  (*p ) [3 ];   // int  () [3 ] int  * (  *p ) [4 ];   // int  * () [4 ]  

Summarize:
1. Type of pointer
From a grammatical point of view, the pointer name is removed from the pointer declaration statement, and the rest is the type of pointer. Is the type that the pointer itself has .

2. The type that the pointer points to
When you access the memory area that the pointer points to by using a pointer, the type that the pointer points to determines what the compiler will look for in that memory area.
Syntactically, you only have to remove the pointer name and the pointer to the left of the name in the pointer declaration, and all that remains is the type that the pointer points to .

* * Pointer expression * *
int a,b;  intarray[10];  int *pa;  //  &a是一个指针表达式。  int **p=&pa;  //  &pa也是一个指针表达式。  *p=&b;  //  *p和&b都是指针表达式。  pa=array;  pa++;  //  这也是指针表达式。
char *array[20];  char **parr=array;//如果把arr看作指针的话,arr也是指针表达式  char *str;  str=*parr;//*parr是指针表达式  str=*(parr+1);//*(parr+1)是指针表达式  str=*(parr+2);//*(parr+2)是指针表达式  

The result of a pointer expression is a pointer, so the pointer expression also has four features that the pointer has: the type of pointer, the type that the pointer points to, the memory area that the pointer is pointing to, and the memory that the pointer itself occupies

Pointers and Arrays


int array[5 ]={ 0 , 1 , 2 , 3 , 4 },value;   ...   Value=array[0 ];//Value=*array;  Value=array[3 ];//value=* (array+3 );  Value=array[4 ];//value=* (array+4 ); The pointer is shifted backwards +δ  
char  * STR  [3 ]={ "Hello,nice to meet you!" ,  "Hi,good night!  ", " Hello World "}; char  s[80 ]; strcpy (s), str  [0 ]);  //or strcpy (S,*STR);  strcpy (S,str  [1 ]);  //or strcpy (s,* (str+1));  strcpy (S,str  [2 ]);  //or written strcpy (s,* (str+2)); 

STR is a three-cell array in which each cell of the array is a pointer to a string. With the pointer array name STR as a pointer, it points to the No. 0 element of the array, which is of type char**, and it points to a type of char *.

STR is also a pointer, type is char, the type that is pointed to is char, and it points to the address of the string "Hello,nice to meet you!" The address of the first character, which is the address of ' H '. Str+1 is also a pointer to the 1th element of the array, whose type is char**, which is the type of char *.

* * Pointers and functions * *


int f1(char*,int);  int (*pf1)(char*,int);  pf1=f1;  ....  int a=(*pf1)("abcdefg",7);   //   通过函数指针调用函数。  
What is a reference


A reference is an alias for an object that is used primarily for function arguments and return value types, and the symbol x& represents a reference of type X

* * Pointer Reference difference * *

The pointer points to a piece of memory whose contents are the address of the memory being referred to, while the reference is the alias of a block of memory, and the reference does not change the point

* * Pointer passing and reference passing * *


1 pointer passing
-The parameter is essentially the way the value is passed, and it 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.
-
2 Reference Delivery
-In the process, the formal parameters of the modulated 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 same and different points for pointers and references * *


    1. Same point:
      • Are the concept of addresses;

The pointer points to a piece of memory whose contents are the address of the referred memory, and the reference is the alias of a block of memory.

    1. Different points:
      • The pointer is an entity, and the reference is only an individual name;
      • References can only be initialized once at the time of definition, immutable, pointers variable, reference "mindedness", pointers "inconstant";
      • The reference does not have a const, the pointer has const,const pointer is immutable, (refers to does not have int& const a This form, but the const int& A is some, the former guideline uses itself namely the alias cannot change, this is of course, therefore does not need this form, The latter refers to the value of the reference can not be changed)
      • The reference cannot be null, the pointer can be empty;
      • 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

A brief analysis of C + + pointers and references

Related Article

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.