C + + pointers and references

Source: Internet
Author: User
Tags define function

1, the difference between the pointer and the reference: (1) non-null difference. The reference cannot point to a null value. (2) The difference of legality. Because the pointer may be empty, you need to test it to prevent it from being empty. (3) The difference can be modified. The reference cannot be modified after initialization. (4) Differences in content. The content of a pointer is a memory address, and the reference is simply an alias for a block of memory. So the size of the pointer is always 4, and the reference size is the same as the original variable (char is 1,int 4). Why is a array of reference not possible?***unlike pointer variables and other variables, references has no storage o F their own. A reference is simply an alias for an object this already exists in memory (allowing you to refer to the object by its mem Ory address). Since They has no storage of their own it is impossible to create an array of references. You can of course create a array of objects, each of the which can then be referenced. You can also has several references to the same object. But you cannot store those references because there are nothing to physically store other than the object itself, which is Already stored. For the same reason you cannot reference references or can you point to references. You can have refer and point to objects (or point to NULL of course).

2. Specify the value of the pointer (specify the address):

struct struc{    int  A;     Char b[];     Double CCC;}
(struc*) 0 means that 0 is coerced into the first address of a STRUC structure. This ((struc*) 0)->b represents the offset of B in the struct. 3, the pointer as a function parameter to remember a little, in the function of any change in the parameters, does not affect the value of the parameter in the original program, sopointers, which are addresses, will not change in the original program.(Just as the pointer is a parameter, the pointer refers to a reference that might be modified in the function). If you want the address to be changed in the original program (for example, using this pointer in a function to request memory), then method I: Pass pointers to pointers, such as:
void GetMemory (charint  num) {    *p = (charmalloc(sizeof(  Char) *num);}
Method II: Returns the pointer as a return value
Char* getmemory (charint  num) {    = (charmalloc (sizeof(char) *num);     return p;}
4, the pointer as the return value if the pointer is newly created in the function, then the pointer itself is present in the stack, and as the function exits, the pointer is freed. So to get the right results, you can: Method I: Apply for global pointers. Global variables are existing in-memory global zones. Method II: Apply for static. This is stored in a static storage space. Such as:
Const Char* stra () {    staticchar str[] ="helloworld" ;     return str;}
5. function pointer (1) Define function
int max (intint  y) {    return x>y?  x:y;}
(2) Declaring a function
int max (int,int);
(3) Declaring function pointers and assigning values
int (*p) (int,int) = &max;
Note that the function pointer must be enclosed in parentheses, or it becomes a function that returns a value of int*. 6, array pointers such as: Int (*a) [10], like a function pointer, the array pointer must also have parentheses, otherwise it becomes an array of elements int* because sizeof (*a) result is 40, so a++ is to move backwards 40 bytes. 7. The pointer and the handle pointer mark the space in which a physical memory address handle points to the address in physical memory where the data is stored. That is, a handle is a pointer to a pointer. After the Windows Memory manager moves the object's location in memory, it saves the address of the object's new address to this handle. 8. The cast of the pointer assumes that both Class A and Class B have an F () function.
New A (); B// the type of PA is coerced to a PB type, but the address of the PA is still the F () that points to class A. Polymorphism formally takes advantage of this principle. 

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.