Pointer and reference, Pointer Reference

Source: Internet
Author: User

Pointer and reference, Pointer Reference

I. pointer

The Pointer Points to a piece of memory, and its content refers to the memory address. Therefore, the pointer is the address.

1. pointer declaration and initialization

Let's look at the example.

Int ival = 1024; int * pi = 0; correct; 0 indicates empty address int * pi0 = 5; error. 5 indicates a specific number and cannot represent the address int * pil; correct, declare, * pil = 5 is not initialized. Correct. The number in the pil address is 5, and * pil points to 5. Similarly, * pil = ival is correct. pil = & ival; is correct, pil is the address, so assigning the 5 address to pil is the correct int * pil = & val; correct pi = pil; correct, that is, the value between addresses is pil = 0; correct, empty address

* P ++ indicates that the value operation is completed first, and then the ++ operation is performed on the address. YesAddress ++. * The operator priority is higher than ++.

(* P) ++ indicates the auto-increment operation of the value pointed to by * p, which isValue ++.


2. void * pointer

Any type of pointer can be directly assigned to it, without the need for forced type conversion.

For example:

void *p1;int *p2;p1 = p2; 

3. pointer to pointer

Simply put, if the address of pointer A exists in Pointer B, then pointer B is the pointer to the pointer.


4. pointer functions and function pointers

4.1 pointer functions refer to functions with pointers. They are essentially a function. A function returns a pointer of a certain type.

Type identifier * function name (parameter table)

Int * f (x, y );

First, it is a function, but the return value of this function is an address value. Function return values must be accepted using pointer variables of the same type.

float *fun();float *p;p = fun(a);


4.2. A function pointer is a pointer variable pointing to a function, that is, a pointer variable.

Int (* f) (int x);/* declare a function pointer */

The pointer to a function contains the address of the function, which can be used to call the function. The Declaration format is as follows:

Type specifier (* function name) (parameter)


Without parentheses, it becomes a prototype declaration of a function that returns an integer pointer.
For example:
Void (* fptr )();
Assign the function address to the function pointer in the following two forms:
Fptr = & Function;
Fptr = Function;
It is not necessary to take the address operator, because a Function Identifier represents its address. If it is a function call, it must be enclosed in parentheses.

Parameter table.

In conclusion, the main difference is that the pointer to a function is a pointer variable (which must be enclosed in parentheses), and the pointer function is a function.


Ii. References

A reference is an alias of a variable (target). The referenced operation is exactly the same as the direct operation on a variable.

Sizeof gets the variable size.

1. Declaration and initialization

Declared method: type identifier & reference name = target variable name;

[Example 1]: int a; int & ra = a; // defines reference ra, which is a reference of variable a, that is, alias
Note:
(1) & this is not an address calculation, but an identifier.

(2) type identifier refers to the type of the target variable.

(3) When declaring a reference, it must be initialized at the same time.

(4) After the reference declaration is complete, it is equivalent that the target variable name has two names, namely, the original name and reference name of the target, and the reference name cannot be used as the alias of other variable names.
Ra = 1; equivalent to a = 1;

(5) declare a reference. Instead of defining a new variable, it only indicates that the reference name is an alias of the target variable name. It is not a data type, so reference

The system does not occupy storage units and does not allocate storage units to references. Therefore, finding the address for the reference is to find the address for the target variable. & Ra and &.

(6) arrays cannot be referenced. An array is a collection composed of several elements, so an array alias cannot be created.


2. Reference an application

2.1 reference as a parameter
An important role of reference is as a function parameter. In the previous C language, function parameters were passed as values. If large data blocks are passed as parameters

The solution is usually a pointer, because this can avoid putting all the data on the stack and improve the program efficiency. But now (C ++) has added the same efficiency

Is used.

{Int p; p = p1; p1 = p2; p2 = p;} main () {int a, B; cin> a> B; // enter, swap (a, B) values of the two variables B; // call the swap function cout using the variables a and B as the real parameters <a <''<B; // output result}

When the above program is running, if the input data is 10 20 and then press enter, the output result is 20 10.

Note:

(1) passing a reference to a function has the same effect as passing a pointer.

(2) using the reference parameter to pass the function does not generate a copy of the real parameter in the memory. It directly operates on the real parameter.


2.2. Common references

Common Reference declaration method: const type identifier & reference name = target variable name;

The reference declared in this way cannot be modified by reference to the value of the target variable, so that the referenced target becomes const, achieving the security of reference.

Int a; const int & ra = a; ra = 1; // error a = 1; // correct

2.3 reference as return value

To return a function value by reference, the function must be defined in the following format:

Type identifier & function name (parameter list and type description)
{Function body}

Note:
(1) to reference the returned function value. to define a function, add & before the function name &.
(2) The biggest benefit of returning a function value with a reference is that a copy of the returned value is not generated in the memory.


3. value transfer, pointer transfer, and reference Transfer

Parameters cannot be exchanged when values are passed.

Parameters can be exchanged for pointer passing and reference passing. However, pointer transfer is a copy transfer, which wastes memory; reference transfer saves memory.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.